.NET 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,我建议在调试器中运行此代码并打开一个“异常中断”来帮助隔离导致错误的行。这是一种有用的调试技术,可以帮助您将来更快地发现此类问题。转到
调试>>; VS 中的异常
,并选中抛出
列中的公共语言运行时异常
复选框。现在解决您的问题。
sender
可能会作为null
传入。如果是这样,则该行:将抛出
NullReferenceException
。相反,您可以使用:来标识通用参数的类型,而不需要它的实例。
现在,如果不了解您的代码想要做什么,就不可能判断实例化
T
实例是否是正确的做法。 仅仅因为 ReSharper 建议添加where T : new()
并不意味着它是合适的 - 除非您知道这是正确的行为。最后,我不知道是否有令人信服的理由使用反射来调用 MethodInOtherClass - 也许有。但由于您是 C# 新手,我会提到,如果类型
T
始终是某个基类型A
的子类,或者始终实现某个接口I
包含您想要调用的方法,您可以简单地应用通用约束来让编译器知道这一点。然后您可以调用该方法,而无需恢复使用反射:更好。
最后一点评论。将参数传递给方法,然后完全忽略它 - 仅在方法中实例化一个实例,这是不常见的。在某些情况下它是合适的 - 但我倾向于将其视为代码气味。如果可能的话,我会尝试摆脱
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 forCommon Language Runtime Exceptions
in theThrown
column.Now for your issue. It's likely that
sender
is passed in asnull
. If so, the line:will throw a
NullReferenceException
. Instead, you can use: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 addingwhere 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 typeT
will always be a subclass of some base typeA
or will always implement some interfaceI
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: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.sender.GetType().ToString()
返回类型名称,不带程序集名称。Type.GetType
需要一个类型名称和程序集名称(除非该类型位于执行程序集或 mscorlib 中)。如果找不到该类型(例如,由于缺少程序集名称),它将返回空引用。尝试将您的代码更改为
或什至只是
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
or even just
您应该能够执行此操作来获取类型:
您没有指定在哪里获取 NullReferenceException,但我想知道 foo 是否返回为 null...
You should just be able to do this to get the type:
You didn't specify where you're getting the NullReferenceException, but I'm wondering if foo is coming back as null...
要么:
sender
为 null,因此sender.GetType()
将失败。foo.GetMethod("MethodInOtherClass")
返回 null,因此populateMethod.Invoke()
将失败。MethodInOtherClass
依赖于某些先决条件(非空引用),因此当这些条件不存在时它将失败。Either:
sender
is null, sosender.GetType()
will fail.foo.GetMethod("MethodInOtherClass")
returns null, sopopulateMethod.Invoke()
will fail.MethodInOtherClass
depends on certain preconditions (non-null references), so it will fail when those are absent.