Question on having operators as values in ENUM in C#
I have a configuration XML file. some nodes are used for computation purpose and I plan to tore the values like below
<OPERATION>>=</OPERATION> <!-- GREATER THAN OR EQUAL --> <VALUE>1</VALUE>
<OPERATION><=</OPERATION> <!-- LESSER THAN OR EQUAL --> <VALUE>1</VALUE>
I use this XML in my C# cla开发者_如何学Pythonss. I was wondering if i can have an ENUM for these operators? Is this possible?
Cheers, Karthik
Assuming you're using XML serialization, you can try to use the XmlEnum
attribute:
public enum Operation
{
[XmlEnum(">")]
GreaterThan,
[XmlEnum("<")]
LessThan,
...
}
I'm not sure how it will behave with these specific characters, though... (I just tried, it works fine)
EDIT
If you're using Linq to XML, you can just decode the character into an enum value by calling a method from within your query:
Operation DecodeOperation(string s)
{
switch(s)
{
case ">":
return Operation.GreaterThan;
case "<":
return Operation.LessThan;
...
default:
return Operation.Unknown; // or throw exception...
}
}
精彩评论