调用我的方法时出现InvalidOperation
异常MethodInfo.Invoke
,因为它具有通用参数。在互联网上搜索了几个小时后,我不知道如何解决这个问题。这是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;
}