开发者

Parsing user provided enumeration values in C#

I'd like for the user to provide an e开发者_Go百科num name, say "Color", and a value, say "red", and tell them whether or not that is a member value of that enumeration, or if the enumeration even exists.

How can I do this?

In the past, I have used Type.GetType("UserProvidedType").Parse / Convert.ChangeType, but this doesn't appear to work when the user provided type is an enumeration. Please see: Parsing to primitive types, based on user input in c# for past solutions that don't appear to work in this context.

Thanks.


var type = Type.GetType("YourNameSpace.Color");
var belongs = Enum.GetNames(type).Any(o => o == "Red");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜