I need to convert Dictionary to object that contain enum but I got error error : cant cast enum and i cant fix it
private static T DictionaryToObject<T>(IDictionary<string, string> dict) where T : new()
{
var t = new T();
PropertyInfo[] properties = t.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
if (!dict.Any(x => x.Key.Equals(property.Name, StringComparison.InvariantCultureIgnoreCase)))
continue;
KeyValuePair<string, string> item = dict.First(x => x.Key.Equals(property.Name, StringComparison.InvariantCultureIgnoreCase));
// Find which property type (int, string, double? etc) the CURRENT property is...
Type tPropertyType = t.GetType().GetProperty(property.Name).PropertyType;
// Fix nullables...
Type newT = Nullable.GetUnderlyingType(tPropertyType) ?? tPropertyType;
// ...and change the type
object newA = Convert.ChangeType(item.Value, newT);
t.GetType().GetProperty(property.Name).SetValue(t, newA, null);
}
return t;
}