【问题标题】:Conditional Variable Scope in C#C# 中的条件变量作用域
【发布时间】:2018-07-18 01:10:14
【问题描述】:

所以这是一个奇怪的问题,有没有办法根据特定条件(例如通过某些属性)修改变量的可见性?

这可能更像是一个设计模式问题,所以请允许我解释一下我的情况:

我有一个具有许多用户可配置值的类(总共 9 个,其中 4 个是有条件的)。但是,其中一些变量仅在满足某些条件时才适用。现在,它们对用户都是可见的。我正在寻找一种方法,可以在每个范围的上下文中在编译时限制某些变量的可见性。我想避免让用户感到困惑,并让他们可能设置某些会被忽略的值。

例子:

属性B 仅在属性Atrue 时适用。如果用户将A 设置为false,则当前范围将失去B 的可见性。

var settings = new Settings() {
    A = true,
    B = ... //Everything is fine since A is true
}


var settings = new Settings() {
    A = false,
    B = ... //Compile Error, Settings does not contain definition for "B"
}

//Somewhere that uses the settings variable...
if(A) { useB(B); } else { useDefault(); }

有没有比“好的文档”更好的解决方案?

【问题讨论】:

  • 是的,我想这也是我能想到的最接近的东西,但主要区别在于它们需要一个常量值,而不是依赖于某个变量或一组变量。跨度>
  • 那是因为它是一个编译时选项。据我所知,您无法在运行时修改可见性。
  • @deleted :我正在寻找编译时动态范围。如果另一个开发人员甚至无法阅读文档,那么我认为调整范围不会有太大帮助。认为这是不可能的(虽然在概念上很酷),但因为总是有新的东西要学,所以我认为问问看 C# 是否有一些聪明的东西不会有什么坏处。
  • OP,这个问题毫无意义。您想根据运行时稍后发生的事情来修改编译时范围?那将如何运作?如果有人编写访问B 的代码,但尚未运行该代码,您是否希望该代码能够编译?那么,当变量在运行时发生变化时,会发生什么?您是否希望应用程序突然停止工作?代码无效时不会自行擦除。

标签: c# design-patterns scope conditional visibility


【解决方案1】:

您无法完全按照您的要求进行操作,但您可以通过构建器模式将流畅的 API 紧密链接起来......

public interface ISettings
{
    string SomePropertyOnlyForTrue { get; }
    int B { get; }
}

public interface IBuilderFoo
{
    IBuilderFooTrue BuildFooTrue();
    IBuilderFooFalse BuildFooFalse();
}

public interface IBuilderFooTrue
{
    IBuilderFooTrue WithSomePropertyOnlyForTrue(string value);
    ISettings Build();
}

public interface IBuilderFooFalse
{
    IBuilderFooFalse WithB(int value);
    ISettings Build();
}

public void Whatever()
{
    var theThingTrue = new BuilderFoo().BuildFooTrue()
        .WithSomePropertyOnlyForTrue("face").Build();
    var theThingTrueCompilerError = new BuilderFoo().BuildFooTrue()
        .WithB(5).Build(); // compiler error

    var theThingFalse = new BuilderFoo().BuildFooFalse()
        .WithB(5).Build();
    var theThingFalseCompilerError = new BuilderFoo().BuildFooFalse()
        .WithSomePropertyOnlyForTrue("face").Build(); // compiler error
}

请注意,getter 仅在 ISettings 中定义,您最好使该类不可变,以在成为 Build() 后不允许更改。我没有为建设者提供 impls,但应该很容易弄清楚。让我知道您是否需要除了构建器示例之外的其他东西,例如 https://www.dofactory.com/net/builder-design-pattern

这是一个简单的例子:https://dotnetfiddle.net/DtEidh

【讨论】:

  • 有趣而聪明的方法。
【解决方案2】:

不,这是不可能的。如果是某种安全问题,请注意,如果您想发疯,您甚至可以致电internal things via reflection

我能想象到的最接近的就是接口:

public interface IA
{
    public bool A { get; set; }
}

public interface IB
{
    public bool B { get; set; }
}

public class Settings: IA, IB
{
    public bool A { get; set; }
    public bool B { get; set; }
}

用法举例:

IA config = new Settings();
config.A = true; //fine
config.B = true; //error

也就是说,如果这是一个问题,您的模型可能包含大量数据。也许AB 可以是单独的类,它们是模型的属性?

public class Settings
{
    public A A {get; set;}
    public B B {get; set;}
}

或者你可以创建一个工厂类

public class SettingsFactory
{
    public Settings CreateA(...)
    {
        return new Settings { ... };
    }

    public Settings CreateB(...)
    {
        return new Settings { ... };
    }
}

无论如何,您应该相信您的用户,他正在阅读您的文档。

【讨论】:

    猜你喜欢
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 2018-01-20
    相关资源
    最近更新 更多