简体   繁体   English

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

[英]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.

相关问题 C# Npgsql 连接检查器异常抛出 System.NullReferenceException '对象引用未设置为 object 的实例。 - C# Npgsql connection checker Exception Throw System.NullReferenceException 'Object reference not set to an instance of an object.' c# linq 异常:未将对象引用设置为对象的实例 - c# linq exception: object reference not set to an instance of an object C#未处理的异常:对象引用未设置为对象的实例 - c# unhandled exception: object reference not set to an instance of an object Type.GetMethod返回“对象引用未设置为对象的实例”。 - Type.GetMethod returns “Object reference not set to an instance of an object.” 对象引用未设置为对象的实例(C#) - Object reference not set to an instance of an object (C#) C#:对象引用未设置为对象的实例 - C#: Object reference not set to an instance of an object C#对象引用未设置为对象的实例 - C# Object reference not set to an instance of an object 对象引用未设置为对象C#的实例 - object reference not set to an instance of an object C# c#:对象引用未设置为对象的实例 - c#: Object reference not set to an instance of an object 对象引用未设置为对象的实例[C#] - Object reference not set to an instance of an object [C#]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM