[英]C# GetMethod throw exception: Object reference not set to an instance of an object
I had a generic method that I'd like to invoke, but it throws the exception below: 我有一个要调用的通用方法,但是它引发以下异常:
Object reference not set to an instance of an object. 你调用的对象是空的。
It's in fact the GetMethod(...) who equals to null. 实际上是GetMethod(...)等于null。 But I can't find where I did wrong cause I had a very similar function in another class. 但是我找不到我做错了什么地方,因为我在另一个类中有一个非常相似的功能。 This is the code simple: 这是简单的代码:
Class DatabaseSyncronizor
{
...
internal void SyncronizeAll()
{
for(int i=0;i<MyList.Count();i++)
{
Type type=Type.GetType(MyModelClass);
typeof(DatabaseSyncronizor).GetMethod("Synchronize",BindingFlags.NonPublic|BindingFlags.Instance).MakeGenericMethod(type).Invoke(this, null);
}
}
private void Syncronize<T>() where T :class,IDate
{
IGenericService<T> service = new GenericService<T>(new UnitOfWorkFactory(_connectionString));
...
}
}
Thank you for you help! 谢谢你的帮助!
Regards, Léona 问候,莱昂纳
Your method is called Syncronize not Synchronize. 您的方法称为“同步不同步”。
Try this 尝试这个
typeof(DatabaseSyncronizor).GetMethod("Syncronize",BindingFlags.NonPublic|BindingFlags.Instance)
.MakeGenericMethod(type).Invoke(this, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.