假设我在一个类中有两个方法,如下所示:
class Foo
{
public void Convert(string s, int x){ }
public void Convert(string s, double x) { }
}
如果我使用:
var method = typeof (Foo)
.GetMethod("Convert", new[] {typeof (string), typeof (int)});
我得到了正确的方法。但是如果我更改 x
并使其成为第一种方法中的 out
参数:
public void Convert(string s, out int x) { }
然后我得到第二种方法Convert(string s, double x)
。
我不明白为什么它不返回第一个方法或至少 null
而不是第二个方法?第二种方法的签名与我提供的类型不匹配。在第二种情况下如何获得正确的方法?有没有办法直接得到它?我知道我可以获得所有方法,然后根据参数类型过滤它们,但我认为应该有一种直接的方法来做到这一点,但我错过了它......
最佳答案
尝试使用 Type
的 MakeByRefType
方法:
var method = typeof (Foo)
.GetMethod("Convert", new[] { typeof(string), typeof(int).MakeByRefType() });
关于c# - 如何获得具有 out 参数的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23159870/