如何投射反映出的结果
从对象到List<TDynamicType>
?
Type tSource = item.SourceType; //enum
Type tDest = item.DestinationType; //enum
MethodInfo method = typeof(EnumConverters).GetMethod("GetEnumValues");
MethodInfo methodGenericSource = method.MakeGenericMethod(tSource);
object enumsSource = methodGenericSource.Invoke(null, null);
// i need to convert enumsSource to List<tDest> (where tDest is enum)
List<tDest> list = ???
是否存在诸如“ getResultGeneric”或“ getResultOfType”之类的反射函数?
答案 0 :(得分:0)
可以尝试像这样{@ {1}}
答案 1 :(得分:0)
我已经解决了这一问题,将对象强制转换为iEnumerableOfInteger(以listOfObject广播为空),然后对其进行解析。
Type tSource = item.SourceType; //enum
Type tDest = item.DestinationType; //enum
MethodInfo method = typeof(EnumConverters).GetMethod("GetEnumValues");
MethodInfo methodGenericSource = method.MakeGenericMethod(tSource);
object objEnumsSource = methodGenericSource.Invoke(null, null);
//var listObj = (IEnumerable<object>)objEnumsSource;//throw
var listInt = (IEnumerable<int>)objEnumsSource;
foreach (var i in listInt)
{
if (!Enum.IsDefined(tSource, i))
throw new ApplicationException($"!Enum.IsDefined({tSource.FullName}, {i})");
var o = Enum.ToObject(tSource, i);
var e = Convert.ChangeType(o, tSource);
}