GetMethod when argument comes by reference
Asked Answered
T

1

8

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?

Teheran answered 17/7, 2015 at 21:50 Comment(0)
D
16

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)});
Deflower answered 17/7, 2015 at 21:57 Comment(1)
Small change, big effect. Exactly what I needed.Yanirayank

© 2022 - 2024 — McMap. All rights reserved.