gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:52:10 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com