我认为您能够做的最好的事情就是将其
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应该使您足够接近,并允许您执行 *** 作。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)