c# - 如何获得具有 out 参数的方法?

标签 c# reflection parameters out

假设我在一个类中有两个方法,如下所示:

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)

enter image description here

我不明白为什么它不返回第一个方法或至少 null 而不是第二个方法?第二种方法的签名与我提供的类型不匹配。在第二种情况下如何获得正确的方法?有没有办法直接得到它?我知道我可以获得所有方法,然后根据参数类型过滤它们,但我认为应该有一种直接的方法来做到这一点,但我错过了它......

最佳答案

尝试使用 TypeMakeByRefType 方法:

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

关于c# - 如何获得具有 out 参数的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23159870/

相关文章:

c# - for循环中的整数溢出导致奇怪的行为

c# - 异常 : Deadlock

c# - 如何使用C#发送邮件?

c# - 为什么 GetMember(string) 返回 MemberInfo 数组?

parameters - SAPUI5 UploadCollection 的请求负载问题

objective-c - Objective C 中可变数量的方法参数——需要一个例子

c# - 一维柏林噪声?

c# - 来自 IEnumerable<object> 的 GenericTypeDefinitionName

java - 使用 Java 8 lambda 表达式打印有关错误的调试信息

SQL - 检索指定记录日期时间字段两侧 X 秒内出现的所有记录