5

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

public class BigClass
{
    public void getSomething(XmlDocument doc, ref CustomObject obj) {...}
    public void getSomething(XmlDocument doc, ref CustomObject obj, string id) {...}
}

此代码来自外部程序集(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)});

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

有没有办法解决这个问题?

4

1 回答 1

12

您需要查找 ref 类型才能获取 MethodInfo。

MethodInfo inf = s.GetMethod("getSomething", new [] {typeof(XmlDocument), typeof(CustomObject).MakeByRefType(), typeof(string)});
于 2015-07-17T21:57:16.983 回答