T>在动作&LT暧昧的方法调用;参数超载 [英] Ambiguous method call with Action<T> parameter overload

查看:119
本文介绍了T>在动作&LT暧昧的方法调用;参数超载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到调用重载方法具有不同的动作<时,一些意外的编译器行为; T> 变化

I encountered some unexpected compiler behaviour when calling overloaded method with different Action<T> variations.

让我们说我有这个类测试和我创建了其在 CallTest 构造函数实例。

Let's say I have this class Test and I'm creating its instance in the CallTest constructor.

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 作为参数我M收到以下错误:

When calling the Test constructor with either TestDecimal or TestLong as a parameter I'm receiving the following error:

调用以下方法或属性之间暧昧:测试(系统.Action<长>)测试(System.Action<十进制>)

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

我的猜测是有长和小数,但没有任何人有任何其他想法可能我做错?有没有什么解决方法吗?

My guess is there's some implicit conversion going on between long and decimal, but does anyone have any other idea what could have I done wrong? Is there any workaround?

推荐答案

当你通过 TestDecimal TestLong 作为参数,你其实传递的方法组的(毕竟,有可能是多个 TestDecimal 法 - 它可能已经过载)。因此,在这两种情况下隐式转换会发生 - 从方法组的一个特定的委托类型的。这两种方法都是这样的适用候选人的(的第7.4.2节)。从的适用候选人的重载解析算法试图找到的的候选人。然而,匹配参数列表状态的时候比较的转换,即如果两位候选人都发生隐式转换它们都不是规则的更好的:

When you pass TestDecimal or TestLong as a parameter you're in fact passing a method group (after all, there could be more than one TestDecimal method - it could have been overloaded). So in both cases implicit conversion occurs - from a method group to a particular delegate type. Both methods are thus applicable candidates (Section 7.4.2). From applicable candidates the overload resolution algorithm tries to find the best candidate. However, the rules of comparing conversions when matching parameter lists state, that if for both candidates implicit conversion occurs neither of them is better:

< A HREF =htt​​p://msdn.microsoft.com/en-us/library/aa691339%28v=vs.71%29.aspx相对=nofollow>第7.4.2.3 :

[...]

否则,既不转换更好。

这就是为什么在你的情况出现混淆。

That's why in your case there is an ambiguity.

解决方法当然是先投明确的参数:

The workaround is of course to first cast the parameter explicitly:

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

这样一个情况下,就没有必要重载时隐式转换(如投后动作< T> 键入将完全一致),其他就必须转换(动作<长> 动作<十进制> ),以及上面提到的部分指出:

This way for one case there will be no need for implicit conversion during overload resolution (as after the cast Action<T> type will match exactly), and the other would have to be converted (Action<long> to Action<decimal>), and the section mentioned above states that:

[...]

如果S是T1,C1是更好的转换。

If S is T1, C1 is the better conversion.

若S是T2,C2是更好的转换。

If S is T2, C2 is the better conversion.

[...]

这篇关于T&gt;在动作&LT暧昧的方法调用;参数超载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆