String to enum conversion in C#

后端 未结 8 2107
孤街浪徒
孤街浪徒 2020-12-03 00:52

I have a combo box where I am displaying some entries like:

Equals
Not Equals 
Less Than
Greater Than

Notice that these strings contain spa

8条回答
  •  日久生厌
    2020-12-03 01:33

    in C#, you can add extension methods to enum types. See http://msdn.microsoft.com/en-us/library/bb383974.aspx

    You could use this approach to add toString(Operation op), fromString(String str) and toLocalizedString(Operation op) methods to your enum types. The method that you use to lookup the particular string depends on your application and should be consistent with what you do in similar cases. Using a dictionary as others have suggested seems like a good first approach as long as you don't need full localization in your app.

提交回复
热议问题