开发者

Store operator and use directly?

I.. just want to try to see if I can do with some new appounch.

I have a txt file with bunch of conditionals comparsion with values, i.e:

Parameter1!=Value2

Then I will read them and put each of them into my "Condition" obj开发者_开发百科ect, and I will able to run the condition result to look put in Parameter1 and see if is not equal to Value2.

I just wonder if there is a type in C# that I cn store the conditon parameter? So in the object there might define as:

operator InputOperator = "!=";

Then I can use it directly in the code:

if (Parameter1 ConditionObj.InputOperator ConditionObj.Value2)
{
 //do stuff
}

I know there is many other appounches, I just wonder if there is something like that exisits in C#.

Thanks in advance.


The closest you could easily do would be a delegate:

// Equivalent to Func<T, T, bool>, but the name probably helps explain
// the expected semantics better.
delegate bool ConditionOperator<T>(T left, T right)

Then you could use:

ConditionOperator<int> notEquals = (x, y) => x != y;

for example. Then you can pass around delegates as normal, and call them when you want to.


You could store a mapping of string => Func<object, object, bool> delegates, and execute the appropriate delegate based on what's read in.


No, you can't do that. I'd say the best bet is to use a method in ConditionObj for the comparison.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜