C#GetMethod抛出异常:未将对象引用设置为对象的实例

Leo*_*ona 1 c# generics

我有一个我想要调用的泛型方法,但它抛出以下异常:

你调用的对象是空的.

事实上,GetMethod(...)等于null.但是我无法找到我做错的地方,因为我在另一个班级里有一个非常相似的功能.这是简单的代码:

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));
         ...
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

此致,Léona

小智 6

您的方法称为Syncronize not Synchronize.

试试这个

typeof(DatabaseSyncronizor).GetMethod("Syncronize",BindingFlags.NonPublic|BindingFlags.Instance)
                           .MakeGenericMethod(type).Invoke(this, null);
Run Code Online (Sandbox Code Playgroud)