开发者

Convert object to enum C#

开发者 https://www.devze.com 2022-12-31 04:16 出处:网络
I have binded a list of enum to a combobox. Now I want to get the SelectedItem return the enum, which currently re开发者_运维问答turns it as type object. How do I convert this object to my enum?

I have binded a list of enum to a combobox. Now I want to get the SelectedItem return the enum, which currently re开发者_运维问答turns it as type object. How do I convert this object to my enum?

My framework is silverlight on windows-phone-7


Cast it directly:

MyEnum selected = (MyEnum)cboCombo.SelectedItem;

Note that you can't use the as cast in this case since an Enum is a value type.


Have you tried this??

YourEnum abc = (YourEnum) Enum.Parse(typeof(YourEnum), yourObject.ToString());
0

精彩评论

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