My code is .Net 4.0 and I am trying to understand some legacy code I now work with. I can't change it at the moment and i'm sure this code has worked before my time. It needs to make an enum
from strings
, but the type is not recognized as an enum
.
EDIT
I now realize the enum
property is actually nullable
. So it is a NetType
? How can I convert that into an enum
if it has a value?
When I debug and see the type that is being checked on the enum
, this is what I see:
FullName = System.Nullable
1[[AppName.Model.NetType, AppName.Model, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]
1
Name = Nullable
This is the enum
:
public enum NetType
{
ChoiceOne = 1,
ChoiceTwo = 2
}
Main code, simplified for clarity:
var property = typeof(MainClass).GetProperty("NetType");
var value = GetValue(property.PropertyType, "ChoiceOne");
private object GetValue(Type type, string valueString)
{
if (type.IsEnum)// Why false?
return Enum.Parse(type, valueString);
if (type == typeof (Int32))
return Int32.Parse(valueString);
return valueString;
}