Parse string to enum type

后端 未结 8 1209
灰色年华
灰色年华 2020-11-29 09:04

I have an enum type like this as an example:

public Enum MyEnum {
    enum1, enum2, enum3 };

I\'ll read a string from config file. What I n

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 09:47

    If you're using .NET 3.5 (or even 2.0, if you trim out the extension method), I've had great luck with the techniques in this article:

    Enumerations and Strings - Stop the Madness!

    EDIT: Domain is gone and is now a link farm. I pulled the code (slightly modified and added to over time) from our codebase at work, which you can now find here:

    https://gist.github.com/1305566

提交回复
热议问题