G
Guest
I am trying to convert the value of a string to a defined enum value such as
follows.
public enum MyEnum { One, Two };
string MyString = "One";
// or even this is fine
string MyString2 = "MyEnum.One";
// This doesn't work (obviously), but effectively this
// is what I am trying to accomplish.
MyEnum ConvertedString = (MyEnum)MyString;
Is there a way using the TYPE object or reflection to accomplish this goal?
I am a pretty advanced developer and understand most topics and commands,
so feel free to offer any ideas.
I know the example is silly and why would anyone do this, but
I am really trying to read these enum values from a text file and
assign them to a variable within a class I have, but it would have
clouded the question if I showed all of that code.
I hope the simple example is enough?
Thanks for any help.
Greg
follows.
public enum MyEnum { One, Two };
string MyString = "One";
// or even this is fine
string MyString2 = "MyEnum.One";
// This doesn't work (obviously), but effectively this
// is what I am trying to accomplish.
MyEnum ConvertedString = (MyEnum)MyString;
Is there a way using the TYPE object or reflection to accomplish this goal?
I am a pretty advanced developer and understand most topics and commands,
so feel free to offer any ideas.
I know the example is silly and why would anyone do this, but
I am really trying to read these enum values from a text file and
assign them to a variable within a class I have, but it would have
clouded the question if I showed all of that code.
I hope the simple example is enough?
Thanks for any help.
Greg