afk*_*afk 7 c# generics implicit-conversion
我正在尝试使用自定义整数类型,遇到了一个涉及泛型,隐式转换和32位整数的有趣问题。
下面是如何重现该问题的精简示例。如果我有两个隐式方法要转换int
为MyInt
,反之亦然,那么我会收到一个编译错误,看起来像C#无法解析要使用的泛型类型。并且仅在int
或时发生uint
。所有其它整数类型做工精细:sbyte
,byte
,short
,ushort
,long
,ulong
。
如果删除隐式转换方法之一,它也可以正常工作。与循环隐式转换有关?
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
下面的测试代码显示了定义两个隐式方法时遇到的编译错误。
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我相信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);
Run Code Online (Sandbox Code Playgroud)
我认为隐式转换没有犯错,因为我可以使其他类似的示例在与32位int一起使用时也会产生相同的问题。
我正在.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.
}
}
Run Code Online (Sandbox Code Playgroud)
与循环隐式转换有关?
是的(尽管您已经证明了其中一项转换被消除时,它可以正常工作,从而证明了这一点)。
int
而不是其他类型发生这种情况的原因是您的文字类型是 int
。这意味着在重载解析期间,编译器可以采用以下两种方式:convert int
to MyInt
或convert MyInt
to int
。两种选择显然都不比另一种“更好”,因此这些转换都不值得考虑。
然后,在排除了该方法的最接近的通用版本之后,在剩下的可用重载中,剩下的唯一一个就是Equal(string, string)
重载(剩下的只有两个参数的唯一重载是Equal<T>(IEnumerable<T>, IEnumerable<T>)
,这比Equal(string, string)
根据重载解析规则)。找到了一种明显比其他方法“更好”的方法,然后编译器尝试将该方法与您的参数一起使用,当然这是不合适的,从而导致产生错误。
另一方面…
尝试致电时Equal(4, myLong)
,您有两种不兼容的类型。具有type int
和MyLong
value的文字。在这种情况下,编译器将逐个尝试每个参数,并发现当将MyLong
类型用作类型参数时,可以将int
文字提升为a long
,然后将其隐式转换为MyLong
。但这不可能相反。无法选择int
作为通用类型参数,因为MyLong
它不能隐式转换为int
。因此,在这种情况下,有是一个“更好”超载可供选择,所以它的选择。
通过显式指定文字的类型,您可以尝试不同的组合并在工作中看到此模式。首先,我希望使用一个更简单的包装器类进行测试:
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 };
}
Run Code Online (Sandbox Code Playgroud)
然后尝试这些:
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
Run Code Online (Sandbox Code Playgroud)
唯一与众不同的int
是,这是数字文字的默认类型。否则,自动换行的类型与自动换行的类型int
完全相同。只要两个参数之间只能在一个方向上进行转换,就可以了。但是,当双向转换都可用时,编译器别无选择,只能举手放弃。