加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用Action参数重载的不明确方法调用

发布时间:2020-12-15 08:12:51 所属栏目:百科 来源:网络整理
导读:当使用不同的Action T调用重??载方法时,我遇到了一些意外的编译器行为.变化. 假设我有这个类Test,我在CallTest构造函数中创建它的实例. public class Test{ public Test(Actionlong arg) { } public Test(Actiondecimal arg) { }}public class CallTest{ pub
当使用不同的Action< T>调用重??载方法时,我遇到了一些意外的编译器行为.变化.

假设我有这个类Test,我在CallTest构造函数中创建它的实例.

public class Test
{
    public Test(Action<long> arg)
    {

    }

    public Test(Action<decimal> arg)
    {

    }
}

public class CallTest
{
    public CallTest()
    {
        Test t = new Test(TestDecimal);
    }

    public void TestDecimal(decimal arg)
    {

    }

    public void TestLong(long arg)
    {

    }    
}

当使用TestDecimal或TestLong作为参数调用Test构造函数时,我收到以下错误:

The call is ambiguous between the following methods or properties: ‘Test(System.Action<long>)‘ and ‘Test(System.Action<decimal>)

我的猜测是在长和十进制之间有一些隐式转换,但是有没有人知道我可能做错了什么?有没有解决方法?

解决方法

当您将TestDecimal或TestLong作为参数传递时,您实际上正在传递一个方法组(毕竟,可能有多个TestDecimal方法 – 它可能已经过载).因此,在这两种情况下都会发生隐式转换 – 从方法组到特定委托类型.因此两种方法都适用于候选者( Section 7.4.2).从适用的候选者中,重载决策算法试图找到最佳候选者.但是,在匹配参数列表状态时比较转换的规则,如果两个候选隐式转换都发生,则两者都不是更好:

Section 7.4.2.3:

[…]

Otherwise,neither conversion is better.

这就是为什么在你的情况下存在歧义.

解决方法当然是首先明确地转换参数:

new Test(new Action<decimal>(TestDecimal))

这种方式对于一种情况,在重载解析期间不需要隐式转换(因为在转换Action< T>类型之后将完全匹配),而另一种情况必须被转换(Action< long> to Action< decimal>),以及上述部分指出:

[…]

If S is T1,C1 is the better conversion.

If S is T2,C2 is the better conversion.

[…]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读