参数来自参考时的GetMethod

Che*_*nto 4 .net c# reflection createinstance getmethod

我正在使用反射创建一个对象的实例,并在对象的类中获取方法,但是当我必须使用类型数组Type来避免歧义问题时,问题就来了,这里是我的代码示例我试图达到.

public class BigClass
{
    public void getSomething(XmlDocument doc, ref CustomObject obj) {...}
    public void getSomething(XmlDocument doc, ref CustomObject obj, string id) {...}
}
Run Code Online (Sandbox Code Playgroud)

此代码来自外部程序集(file.dll),我正在使用下一个代码.

Assembly a = Assembly.LoadFrom("file.dll");
Type s = a.GetType("FileNamespace.BigClass");
MethodInfo inf = s.GetMethod("getSomething", new [] {typeof(XmlDocument), typeof(CustomObject), typeof(string)});
Run Code Online (Sandbox Code Playgroud)

要获取MethodInfo使用3个参数的对象,但变量"inf"为空,我认为因为它没有找到使用"ref"的参数的方法.

有办法解决这个问题吗?

eva*_*anb 6

您需要查找ref类型才能获得MethodInfo.

MethodInfo inf = s.GetMethod("getSomething", new [] {typeof(XmlDocument), typeof(CustomObject).MakeByRefType(), typeof(string)});
Run Code Online (Sandbox Code Playgroud)