32位隐式转换无法通过通用重载解决方案

问题描述 投票:3回答:1

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

下面是如何重现问题的精简示例。如果我有two隐式方法将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);

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

我正在.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.
    }
}

我正在尝试自定义整数类型,并遇到了一个涉及泛型,隐式转换和32位整数的有趣问题。以下是如何复制的简化示例...

c# generics implicit-conversion
1个回答
0
投票

与循环隐式转换有关?

© www.soinside.com 2019 - 2024. All rights reserved.