.NET 2.0:使用反射和泛型调用方法会导致异常

发布于 2024-09-05 15:32:27 字数 659 浏览 2 评论 0原文

我是 Stack Overflow 的新手,请原谅我。我刚刚开始过渡到 C#,但遇到了一个问题。

我想传递一个通用类并调用该类的方法。因此,我的代码如下所示:

public void UpdateRecords<T>(T sender) where T : new() //where T: new() came from Resharper
{
    Type foo = Type.GetType(sender.GetType().ToString());
    object[] userParameters = new object[2];
    userParameters[0] = x;
    userParameters[1] = y;
    sender = new T(); //This was to see if I could solve my exception problem
    MethodInfo populateRecord = foo.GetMethod("MethodInOtherClass");
    populateMethod.Invoke(sender, userParameters);
}

抛出异常:“对象引用未设置为对象的实例。”

再次,我真的很抱歉,因为我对 C# 几乎是全新的,这是我第一次处理反射和泛型。谢谢你!

I'm new to Stack Overflow, so forgive me. I've just started transititoning over to C# and I am stuck on a problem.

I am wanting to pass a generic class in and call a method from that class. So, my code looks as such:

public void UpdateRecords<T>(T sender) where T : new() //where T: new() came from Resharper
{
    Type foo = Type.GetType(sender.GetType().ToString());
    object[] userParameters = new object[2];
    userParameters[0] = x;
    userParameters[1] = y;
    sender = new T(); //This was to see if I could solve my exception problem
    MethodInfo populateRecord = foo.GetMethod("MethodInOtherClass");
    populateMethod.Invoke(sender, userParameters);
}

Exception thrown: "Object reference not set to an instance of an object."

Again, I really apologize, as I am nearly brand new to C# and this is the first time I've ever handled reflection and generics. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

殊姿 2024-09-12 15:32:27

首先,我建议在调试器中运行此代码并打开一个“异常中断”来帮助隔离导致错误的行。这是一种有用的调试技术,可以帮助您将来更快地发现此类问题。转到调试>>; VS 中的异常,并选中抛出 列中的公共语言运行时异常 复选框。

现在解决您的问题。 sender 可能会作为 null 传入。如果是这样,则该行:

Type foo = Type.GetType(sender.GetType().ToString()); 

将抛出 NullReferenceException。相反,您可以使用:

Type foo = typeof(T); 

来标识通用参数的类型,而不需要它的实例。

现在,如果不了解您的代码想要做什么,就不可能判断实例化 T 实例是否是正确的做法。 仅仅因为 ReSharper 建议添加 where T : new() 并不意味着它是合适的 - 除非您知道这是正确的行为

最后,我不知道是否有令人信服的理由使用反射来调用 MethodInOtherClass - 也许有。但由于您是 C# 新手,我会提到,如果类型 T 始终是某个基类型 A 的子类,或者始终实现某个接口 I 包含您想要调用的方法,您可以简单地应用通用约束来让编译器知道这一点。然后您可以调用该方法,而无需恢复使用反射:

public void UpdateRecords<T>(T sender) 
   where T : SomeBaseClass_Or_SomeInterface_ThatDefinesMethod
{
  sender = new T();
  sender.MethodInOtherClass( x, y );
}

更好。

最后一点评论。将参数传递给方法,然后完全忽略它 - 仅在方法中实例化一个实例,这是不常见的。在某些情况下它是合适的 - 但我倾向于将其视为代码气味。如果可能的话,我会尝试摆脱 sender 参数,或者更改代码以首先测试它是否为空,然后再实例化。

First of all, I would recommend running this code in the debugger and turning one "Break on Exception" to help isolate which line causes the error. This is a useful debugging technique that can help you find these types of problems more quickly in the future. Go to Debug >> Exceptions in VS and check the checkbox for Common Language Runtime Exceptions in the Thrown column.

Now for your issue. It's likely that sender is passed in as null. If so, the line:

Type foo = Type.GetType(sender.GetType().ToString()); 

will throw a NullReferenceException. Instead, you can use:

Type foo = typeof(T); 

which identifies the type of the generic paramter without requiring an instance of it.

Now, without knowing more about what your code is trying to do, it's impossible to say whether instantiating an instance of T is the right thing to do. Just because ReSharper recommends adding where T : new() doesn't mean it's appropriate - unless you know that that's the right behavior.

Finally, I don't know if there is a compelling reason for using reflection to invoke MethodInOtherClass - perhaps there is. But since you're new to C#, I'll mention that if the type T will always be a subclass of some base type A or will always implement some interface I that includes the method you want to call, you can simply apply a generic constraint to let the compiler know this. Then you can call the method without reverting to using reflection:

public void UpdateRecords<T>(T sender) 
   where T : SomeBaseClass_Or_SomeInterface_ThatDefinesMethod
{
  sender = new T();
  sender.MethodInOtherClass( x, y );
}

Much nicer.

One final comment. It's unusual to pass in an argument to a method, and then ignore it completely - only to instantiate an instance within the method. There are cases when it's appropriate - but I tend to view it as a code smell. If possible, I would try to either get rid of the sender argument, or change the code to first test it for null and instantiate only then.

苄①跕圉湢 2024-09-12 15:32:27

sender.GetType().ToString() 返回类型名称,不带程序集名称。

Type.GetType 需要一个类型名称程序集名称(除非该类型位于执行程序集或 mscorlib 中)。如果找不到该类型(例如,由于缺少程序集名称),它将返回空引用。


尝试将您的代码更改为

Type foo = sender.GetType();

或什至只是

Type foo = typeof(T);

sender.GetType().ToString() returns the type name without the assembly name.

Type.GetType expects a type name with an assembly name (unless the type is in the executing assembly or mscorlib). If the type cannot be found (e.g., due to the missing assembly name), it returns a null reference.


Try changing your code to

Type foo = sender.GetType();

or even just

Type foo = typeof(T);
死开点丶别碍眼 2024-09-12 15:32:27

您应该能够执行此操作来获取类型:

Type foo = typeof(T);

您没有指定在哪里获取 NullReferenceException,但我想知道 foo 是否返回为 null...

You should just be able to do this to get the type:

Type foo = typeof(T);

You didn't specify where you're getting the NullReferenceException, but I'm wondering if foo is coming back as null...

云淡月浅 2024-09-12 15:32:27

要么:

  1. sender 为 null,因此 sender.GetType() 将失败。
  2. foo.GetMethod("MethodInOtherClass") 返回 null,因此 populateMethod.Invoke() 将失败。
  3. MethodInOtherClass 依赖于某些先决条件(非空引用),因此当这些条件不存在时它将失败。

Either:

  1. sender is null, so sender.GetType() will fail.
  2. foo.GetMethod("MethodInOtherClass") returns null, so populateMethod.Invoke() will fail.
  3. MethodInOtherClass depends on certain preconditions (non-null references), so it will fail when those are absent.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文