C# 中的条件变量作用域
Conditional Variable Scope in C#
所以这是一个奇怪的问题,是否有任何方法可以根据特定条件(例如通过某些属性)修改变量的可见性?
这可能更像是一个设计模式问题,所以请允许我解释一下我的情况:
我有一个 class,它有许多用户可配置的值(总共 9 个,其中 4 个是有条件的)。但是,其中一些变量仅在满足某些条件时才适用。现在,它们对用户都是可见的。我正在寻找一种方法,可以在编译时在每个范围的上下文中限制某些变量的可见性。我想避免让用户感到困惑,并避免让他们设置可能会被忽略的某些值。
示例:
属性 B
仅适用于 属性 A
为 true
的情况。如果用户将 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(); }
有没有比"good documentation?"
更好的解决方案
不,这是不可能的。如果是某种安全问题,请注意,如果您想发疯,您甚至可以调用 internal
东西 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
也就是说,如果这是一个问题,您的模型可能包含太多数据。也许 A
和 B
可以是分开的 classes,它们是您模型的属性?
public class Settings
{
public A A {get; set;}
public B B {get; set;}
}
或者您可以创建工厂 class
public class SettingsFactory
{
public Settings CreateA(...)
{
return new Settings { ... };
}
public Settings CreateB(...)
{
return new Settings { ... };
}
}
无论如何,您应该相信您的用户,他正在阅读您的文档。
你不能完全你所要求的,但你可以通过构建器模式获得紧密链接的流畅 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
}
请注意,吸气剂仅在 ISettings
中定义,您最好使 class 不可变,以不允许在成为 Build()
后进行更改。我没有为构建者提供暗示,但应该很容易弄清楚。让我知道您是否确实需要 https://www.dofactory.com/net/builder-design-pattern.
等构建器示例之外的其他内容
这是一个简单的例子:https://dotnetfiddle.net/DtEidh
所以这是一个奇怪的问题,是否有任何方法可以根据特定条件(例如通过某些属性)修改变量的可见性?
这可能更像是一个设计模式问题,所以请允许我解释一下我的情况:
我有一个 class,它有许多用户可配置的值(总共 9 个,其中 4 个是有条件的)。但是,其中一些变量仅在满足某些条件时才适用。现在,它们对用户都是可见的。我正在寻找一种方法,可以在编译时在每个范围的上下文中限制某些变量的可见性。我想避免让用户感到困惑,并避免让他们设置可能会被忽略的某些值。
示例:
属性 B
仅适用于 属性 A
为 true
的情况。如果用户将 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(); }
有没有比"good documentation?"
更好的解决方案不,这是不可能的。如果是某种安全问题,请注意,如果您想发疯,您甚至可以调用 internal
东西 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
也就是说,如果这是一个问题,您的模型可能包含太多数据。也许 A
和 B
可以是分开的 classes,它们是您模型的属性?
public class Settings
{
public A A {get; set;}
public B B {get; set;}
}
或者您可以创建工厂 class
public class SettingsFactory
{
public Settings CreateA(...)
{
return new Settings { ... };
}
public Settings CreateB(...)
{
return new Settings { ... };
}
}
无论如何,您应该相信您的用户,他正在阅读您的文档。
你不能完全你所要求的,但你可以通过构建器模式获得紧密链接的流畅 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
}
请注意,吸气剂仅在 ISettings
中定义,您最好使 class 不可变,以不允许在成为 Build()
后进行更改。我没有为构建者提供暗示,但应该很容易弄清楚。让我知道您是否确实需要 https://www.dofactory.com/net/builder-design-pattern.
这是一个简单的例子:https://dotnetfiddle.net/DtEidh