BitInteger 我来自System.Numerics
. 此代码抛出InvalidCastException: The specified cast is invalid on the second line。
BigInteger bigint1 = (BigInteger)3455;
BigInteger bigint2 = (BigInteger)(ValueType)3455;
一切都会好起来的,但他们在msdn上做,一切都井井有条。我究竟做错了什么?
基本上,我正在尝试编写一个像“To”这样的演员表操作符。对于类,一切都很简单,但对于结构,我正在尝试实现这个方法:
public static T To<T>(this ValueType o)
然而,在 msdn 上有这样的代码:
public static NumericRelationship Compare(ValueType value1, ValueType value2)
{
// Use BigInteger as common integral type
if (IsInteger(value1) && IsInteger(value2)) {
BigInteger bigint1 = (BigInteger) value1;
BigInteger bigint2 = (BigInteger) value2;
return (NumericRelationship) BigInteger.Compare(bigint1, bigint2);
}
}
public static bool IsInteger(ValueType value)
{
return (value is SByte || value is Int16 || value is Int32
|| value is Int64 || value is Byte || value is UInt16
|| value is UInt32 || value is UInt64
|| value is BigInteger);
}
他们导致 BigInteger,为什么这段代码对我不起作用?
抛出异常,因为在我的代码中,当转换为 valuetype 时,在那里输入了 int,然后我尝试从那里获取 BigInteger。BigInteger 不是 int 的超类型,因此会出现转换错误。向下转型。元尼特| 微软