开发者

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>&GT;=</OPERATION> <!-- GREATER THAN OR EQUAL --> <VALUE>1</VALUE>
<OPERATION>&LT;=</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...
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜