32 位隐式转换无法通过通用重载解析

32 bit implicit conversions fail generic overload resolution

我正在试验自定义整数类型,遇到了一个涉及泛型、隐式转换和 32 位整数的有趣问题。

下面是如何重现问题的精简示例。如果我有 两个 int 转换为 MyInt 的隐式方法,反之亦然,我会得到一个编译错误,看起来像 C# 无法解析哪个泛型类型采用。它 只发生 intuint。所有其他整数类型都可以正常工作:sbytebyteshortushortlongulong.

如果我删除其中一种隐式转换方法,它也可以正常工作。与循环隐式转换有关?

using Xunit;

public class MyInt
{
    public int Value;

    //If I remove either one of the implicit methods below, it all works fine.
    public static implicit operator int(MyInt myInt)
    {
        return myInt.Value;
    }
    public static implicit operator MyInt(int i)
    {
        return new MyInt() { Value = i };
    }

    public override bool Equals(object obj)
    {
        if (obj is MyInt myInt)
        {
            return this.Value == myInt.Value;
        }
        else
        {
            int other_int = (int)obj;
            return Value == other_int;
        }
    }
}

下面的测试代码显示了定义两个隐式方法时出现的编译错误。

public class Test
{
    [Fact]
    public void EqualityTest()
    {
        MyInt myInt = new MyInt();
        myInt.Value = 4 ;

        Assert.Equal(4, myInt.Value);   //Always OK which makes sense

        //Compile errors when both implicit methods defined:
        //  Error CS1503  Argument 1: cannot convert from 'int' to 'string', 
        //  Error CS1503  Argument 2: cannot convert from 'ImplicitConversion.MyInt' to 'string'
        Assert.Equal(4, myInt);
    }
}

我相信 C# 抱怨无法将两种类型都转换为字符串,因为这是最后一个 Xunit.Assert.Equal() 重载的类型,而所有其他类型都无法匹配:

    //Xunit.Assert.Equal methods:
    public static void Equal<T>(T expected, T actual);
    public static void Equal(double expected, double actual, int precision);
    public static void Equal<T>(T expected, T actual, IEqualityComparer<T> comparer);
    public static void Equal(decimal expected, decimal actual, int precision);
    public static void Equal(DateTime expected, DateTime actual, TimeSpan precision);
    public static void Equal<T>(IEnumerable<T> expected, IEnumerable<T> actual, IEqualityComparer<T> comparer);
    public static void Equal<T>(IEnumerable<T> expected, IEnumerable<T> actual);
    public static void Equal(string expected, string actual, bool ignoreCase = false, bool ignoreLineEndingDifferences = false, bool ignoreWhiteSpaceDifferences = false);
    public static void Equal(string expected, string actual);

我不认为我在隐式转换上犯了错误,因为我可以让其他 similar examples 在与 32 位整数一起使用时产生同样的问题。

我正在 .NET Core 3.0 项目中进行测试。

如有任何帮助,我们将不胜感激。谢谢!

澄清: 我想知道的是为什么这只对 32 位整数失败。当类型与下面使用 long.

的示例类似时,隐式转换正在工作(通过调试确认)
using Xunit;

public class MyLong
{
    public long Value;

    public static implicit operator long(MyLong myInt)
    {
        return myInt.Value;
    }
    public static implicit operator MyLong(long i)
    {
        return new MyLong() { Value = i };
    }

    public override bool Equals(object obj)
    {
        if (obj is MyLong myInt)
        {
            return this.Value == myInt.Value;
        }
        else
        {
            long other_int = (long)obj;
            return Value == other_int;
        }
    }
}

public class Test2
{
    [Fact]
    public void EqualityTest()
    {
        MyLong myLong = new MyLong();
        myLong.Value = 4 ;
        Assert.Equal(4, myLong); //NOTE! `4` is implicitly converted to a MyLong 
                                 //object for comparison. Confirmed with debugging.
    }
}

Something to do with circular implicit conversions?

是的(不过,您已经通过证明在消除其中一个转换时它工作正常来证明了这一点)。

这种情况发生在 int 而不是其他类型的原因是你的文字 的类型是 int。这意味着在重载解析期间,编译器可以选择以下任一方式:将 int 转换为 MyInt,或将 MyInt 转换为 int。两种选择都明显 "better" 不同于另一种,因此这些转换都没有考虑。

然后,在排除了方法的最接近的可能泛型版本之后,在剩余可用的重载中,唯一剩下的是 Equal(string, string) 重载(唯一剩下的只有两个参数的是 Equal<T>(IEnumerable<T>, IEnumerable<T>),根据重载解析规则,这比 Equal(string, string) 重载 "worse"。找到一个明显比其他任何方法 "better" 的方法后,编译器然后尝试将该方法与您的参数一起使用,这当然不适合,导致发出错误。

另一方面……

当您尝试调用 Equal(4, myLong) 时,您有两个不兼容的类型。具有类型 intMyLong 值的文字。在这种情况下,编译器逐一尝试每个参数,发现当它使用 MyLong 类型作为类型参数时,可以将 int 文字提升为 long 并且然后将其隐式转换为 MyLong。但它不能走另一条路。不能将select int 作为泛型类型参数,因为MyLong 不能隐式转换为int。所以在那种情况下,一个"better"重载可供选择,所以它被选中了。

通过显式指定文字的类型,您可以尝试不同的组合并在工作中查看此模式。首先,我更喜欢使用更简单的包装器 class 来测试:

public class Wrapper<T>
{
    public T Value;

    public static implicit operator T(Wrapper<T> wrapper) => wrapper.Value;
    public static implicit operator Wrapper<T>(T value) => new Wrapper<T> { Value = value };
}

然后试试这些:

Wrapper<int> w1 = new Wrapper<int> { Value = 4 };
Wrapper<long> w2 = new Wrapper<long> { Value = 4 };

Assert.Equal(4, w1);        // error
Assert.Equal((short)4, w1); // no error
Assert.Equal(4, w2);        // no error
Assert.Equal(4L, w2);       // error

唯一使 int 与众不同的是它是数字文字的默认类型。否则,包装 int 的类型与包装其他任何类型的类型完全相同。只要两个参数之间只能在一个方向上进行转换,一切都很好。但是当双向都可以转换的时候,编译器只好举手放弃