需要根据多种条件将值设置为 属性 的方法
Need approach for Setting value to a property based on numerous conditions
我有许多房产分布在 类 中。需要根据许多条件(每个 属性 大约 5 到 8)分配这些属性的值。我正在寻找许多 'if else' 条件的替代方案。
我遇到 'Rule Engine' 迟到了,但据我所知,它可用于 验证 规则。
任何设计建议都会有很大帮助。
我不确定这是否是 "better" 解决方案适合您,但我会尽力解释。
Value to these properties need to be assigned based on a number of conditions (around 5 to 8 for each property).
我想你的意思是你总是需要写这个,这很烦人:
if (condition1 && condition2 && condition3 && condition4 && condition5) {
Property1 = Value1;
}
if (condition1 && condition2 && condition3 && condition4 && condition5) {
Property2 = Value2;
}
// ...
我想也许这个方法可以解决你的问题?
public static void SetValueForPropertyIf<T>(Predicate<object>[] conditions, ref T property, T value) {
foreach (var predicate in conditions) {
if (!predicate(null)) {
return;
}
}
property = value;
}
并且您可以只使用 lambda 表达式列表调用该方法,忽略参数(因为它始终为 null)、要通过引用传递的变量以及满足所有条件时要设置的值。
但是,这只适用于变量,因为我很确定属性不能通过引用传递(使用 ref
关键字)。所以你必须像这样声明你的属性:
private int someVariable;
public int SomeVariable {
get {return someVariable;}
set {someVariable = value;}
}
如果您不喜欢 Predicate
委托的参数未被使用,请定义您自己的委托!
public delegate bool MyDelegate();
这里有一个如何使用这个方法的例子,以防你不明白我的意思。
class MyClass {
private int someVariable;
public int SomeVariable {
get {return someVariable;}
set {someVariable = value;}
}
public MyClass() {
someVariable = 10;
MyDelegate[] conditions = {
(() => 7 < 10),
(() => 77 == 77),
(() => "Sweeper is awesome".Contains("Sweeper")),
(() => String.IsNullOrEmpty(""))
};
SetValueForPropertyIf(conditions, ref someVariable, 20);
}
}
在这个class的构造函数中,我首先创建了一些条件,这些条件都是真的。然后我用这些条件调用方法。请注意,我使用 someVariable
(字段)而不是 SomeVariable
(属性)作为 ref
参数。
然后你可以打印SomeVariable
:
MyClass mc = new MyClass();
Console.WriteLine(mc.SomeVariable);
输出为 20。万岁!
我有许多房产分布在 类 中。需要根据许多条件(每个 属性 大约 5 到 8)分配这些属性的值。我正在寻找许多 'if else' 条件的替代方案。
我遇到 'Rule Engine' 迟到了,但据我所知,它可用于 验证 规则。
任何设计建议都会有很大帮助。
我不确定这是否是 "better" 解决方案适合您,但我会尽力解释。
Value to these properties need to be assigned based on a number of conditions (around 5 to 8 for each property).
我想你的意思是你总是需要写这个,这很烦人:
if (condition1 && condition2 && condition3 && condition4 && condition5) {
Property1 = Value1;
}
if (condition1 && condition2 && condition3 && condition4 && condition5) {
Property2 = Value2;
}
// ...
我想也许这个方法可以解决你的问题?
public static void SetValueForPropertyIf<T>(Predicate<object>[] conditions, ref T property, T value) {
foreach (var predicate in conditions) {
if (!predicate(null)) {
return;
}
}
property = value;
}
并且您可以只使用 lambda 表达式列表调用该方法,忽略参数(因为它始终为 null)、要通过引用传递的变量以及满足所有条件时要设置的值。
但是,这只适用于变量,因为我很确定属性不能通过引用传递(使用 ref
关键字)。所以你必须像这样声明你的属性:
private int someVariable;
public int SomeVariable {
get {return someVariable;}
set {someVariable = value;}
}
如果您不喜欢 Predicate
委托的参数未被使用,请定义您自己的委托!
public delegate bool MyDelegate();
这里有一个如何使用这个方法的例子,以防你不明白我的意思。
class MyClass {
private int someVariable;
public int SomeVariable {
get {return someVariable;}
set {someVariable = value;}
}
public MyClass() {
someVariable = 10;
MyDelegate[] conditions = {
(() => 7 < 10),
(() => 77 == 77),
(() => "Sweeper is awesome".Contains("Sweeper")),
(() => String.IsNullOrEmpty(""))
};
SetValueForPropertyIf(conditions, ref someVariable, 20);
}
}
在这个class的构造函数中,我首先创建了一些条件,这些条件都是真的。然后我用这些条件调用方法。请注意,我使用 someVariable
(字段)而不是 SomeVariable
(属性)作为 ref
参数。
然后你可以打印SomeVariable
:
MyClass mc = new MyClass();
Console.WriteLine(mc.SomeVariable);
输出为 20。万岁!