Search code examples
c#.netreflectioncreateinstancegetmethod

GetMethod when argument comes by reference


I'm creating an instance of an object using reflection and getting the methods within the class of the object, but then the problem comes when I have to use an array of type Type to avoid ambiguity problems, here is an example of the code that I'm trying to reach.

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

This code comes from an external assembly (file.dll), and I'm using the next code.

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

To get the MethodInfo of the object that uses 3 arguments, but the variable "inf" comes null, I think because it doesn't find the method for the argument that uses "ref".

Is there a way to solve this?


Solution

  • You'll need to look for the ref type in order to get the MethodInfo.

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