How can I get a method with out parameter?

Suppose I have two methods in a class:

class Foo
{
    public void Convert(string s, int x){ }

    public void Convert(string s, double x) { }
}

If I use:

var method = typeof (Foo)
            .GetMethod("Convert", new[] {typeof (string), typeof (int)});

I get the correct method. But if I change xand make it a parameter outin the first method:

public void Convert(string s, out int x) { }

Then I get the second method Convert(string s, double x).

enter image description here

I don’t understand why it does not return the first method, or at least nullinstead of the second? The signature of the second method does not match the types that I provide. How can I get the correct method in the second case? Is there any way to get this straight? I know that I can get all the methods and then filter them based on parameter types, but I think there should be a direct way to do this, and I will skip it ...

+4
2

MakeByRefType Type:

var method = typeof (Foo)
            .GetMethod("Convert", new[] { typeof(string), typeof(int).MakeByRefType() });
+4

, null, , double int, .

, int, . DateTime, null, float, , int float.

+1

Source: https://habr.com/ru/post/1537126/


All Articles