【发布时间】:2013-01-04 14:49:10
【问题描述】:
在我的方法上调用MethodInfo.Invoke 时,我得到一个InvalidOperation 异常,这是因为它具有通用参数。在互联网上搜索了几个小时后,我不知道如何解决这个问题。这是MethodInfo:
object value = null;
if (propertyType.IsClass)
{
Type primaryKeyType = propertyType.GetPrimaryKeyType();
object primaryKeyValue = property.Value.ToValue(primaryKeyType);
MethodInfo GetEntityMethodInfo = typeof(ReportSettingsExtensions)
.GetMethod("GetEntity", BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.NonPublic);
object entity = propertyType;
GetEntityMethodInfo.Invoke(entity, new object[] { primaryKeyValue });
value = entity.GetPrimaryKey();
}
方法如下:
private static T GetEntity<T>(object primaryKeyValue)
{
T entity = default(T);
new Storage(storage =>
{
entity = storage.Create<T>();
entity.SetPrimaryKey(primaryKeyValue);
storage.Load(entity);
});
return entity;
}
【问题讨论】:
-
我曾经问过一个类似的问题,得到了很好的回答:stackoverflow.com/questions/6333896/…
标签: c# reflection