我试图执行一些独立于数字类型的通用数字操作.但是,我知道无法使用泛型来做到这一点.第一个想法是使用where语句过滤传入的类型,但所有数字类型都是关闭的,因此对于通用过滤器无效.此外,泛型不允许标准的数字运算(加法,移位等),因此我能想出的唯一解决方案是非通用地重写每个方法.还有其他想法吗?
作为参考,以下是我最初尝试的代码:
private const int BYTE_SIZE = 8; private const int UINT16_SIZE = 16; private const int UINT32_SIZE = 32; private const int UINT64_SIZE = 64; public static byte[] ToBytes(UInt16[] pnaValues) { return ToSmaller<byte,UInt16>(pnaValues,BYTE_SIZE,UINT16_SIZE); } public static byte[] ToBytes(UInt32[] pnaValues) { return ToSmaller<byte,UInt32>(pnaValues,UINT32_SIZE); } .... public static UInt16[] ToUInt16s(byte[] pnaValues) { return ToLarger<UInt16,byte>(pnaValues,UINT16_SIZE,BYTE_SIZE); } public static UInt16[] ToUInt16s(UInt32[] pnaValues) { return ToSmaller<UInt16,UINT32_SIZE); } ... public static UInt64[] ToUInt64s(UInt32[] pnaValues) { return ToLarger<UInt64,UINT64_SIZE,UINT32_SIZE); } private static TLarger[] ToLarger<TLarger,TSmaller>(TSmaller[] pnaSmaller,int pnLargerSize,int pnSmallerSize) where TLarger : byte,UInt16,UInt32,UInt64 where TSmaller : byte,UInt64 { TLarger[] lnaRetVal = null; int lnSmallerPerLarger = pnLargerSize / pnSmallerSize; System.Diagnostics.Debug.Assert((pnLargerSize % pnSmallerSize) == 0); if (pnaSmaller != null) { System.Diagnostics.Debug.Assert((pnaSmaller % lnSmallerPerLarger) == 0); lnaRetVal = new TLarger[pnaSmaller.Length / lnSmallerPerLarger]; for (int i = 0; i < lnaRetVal.Length; i++) { lnaRetVal[i] = 0; for (int j = 0; j < lnSmallerPerLarger; j++) { lnaRetVal[i] = (lnaRetVal[i] << pnLargerSize) + pnaSmaller[i * lnSmallerPerLarger + j]; } } } return lnaRetVal; } private static TSmaller[] ToSmaller<TSmaller,TLarger>(TLarger[] pnaLarger,int pnSmallerSize,int pnLargerSize) where TSmaller : byte,UInt64 where TLarger : byte,UInt64 { TSmaller[] lnaRetVal = null; int lnSmallerPerLarger = pnLargerSize / pnSmallerSize; System.Diagnostics.Debug.Assert((pnLargerSize % pnSmallerSize) == 0); if (pnaSmaller != null) { lnaRetVal = new TSmaller[pnaLarger.Length * lnSmallerPerLarger]; for (int i = 0; i < lnaRetVal.Length; i++) { for (int j = 0; j < lnSmallerPerLarger; j++) { lnaRetVal[i * lnSmallerPerLarger + (lnSmallerPerLarger - 1 - j)] = pnaLarger[i] >> (j * pnLargerSize); } } } return lnaRetVal; }
解决方法
数字类型实现的算术运算没有通用接口.
Generic operators可能有助于解决您的问题.