C#中泛型类的算术运算符重载
给定一个通用的类定义,例如
public class ConstrainedNumber<T> : IEquatable<ConstrainedNumber<T>>,
IEquatable<T>,
IComparable<ConstrainedNumber<T>>,
IComparable<T>,
IComparable where T:struct, IComparable, IComparable<T>, IEquatable<T>
如何定义算术运算符?
以下内容无法编译,因为’+’运算符无法应用于类型’T’和’T’:
public static T operator +( ConstrainedNumber<T> x, ConstrainedNumber<T> y){
return x._value + y._value;
}
如您所见,通用类型’T’受到’where’关键字的约束,但是对于具有算术运算符(IArithmetic?)的数字类型,我需要一个约束。
“ T”将是原始数字类型,例如int,float等。此类类型是否存在“ where”约束?
回答:
我认为您能够做的最好的事情就是将其IConvertible
用作约束并执行以下操作:
public static operator T +(T x, T y) where T: IConvertible
{
var type = typeof(T);
if (type == typeof(String) ||
type == typeof(DateTime)) throw new ArgumentException(String.Format("The type {0} is not supported", type.FullName), "T");
try { return (T)(Object)(x.ToDouble(NumberFormatInfo.CurrentInfo) + y.ToDouble(NumberFormatInfo.CurrentInfo)); }
catch(Exception ex) { throw new ApplicationException("The operation failed.", ex); }
}
但这不会阻止某人传递String或DateTime,因此您可能需要进行一些手动检查-但是IConvertible应该使您足够接近,并允许您执行操作。
以上是 C#中泛型类的算术运算符重载 的全部内容, 来源链接: utcz.com/qa/427691.html