My .Net 4.0 code. Im trying to understand some legacy code i now work with. I cant change it at the moment and im sure this code has worked before my time. It needs to make an enum from strings. But the type is not recognized as en enum.
EDIT: I now realise the enum property is actually nullable. So its 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 if 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;
}
Aucun commentaire:
Enregistrer un commentaire