J'essaie d'effectuer une manipulation de nombre générique indépendante du type de nombre. Cependant, je ne connais aucun moyen d'utiliser des génériques pour le faire. La première idée consistait à filtrer les types entrants avec une instruction where, mais tous les types de nombres sont fermés et ne sont donc pas valides pour un filtre générique. De plus, les génériques n'autorisent pas les opérations numériques standard (ajout, décalage, etc.), de sorte que la seule solution que je puisse proposer est de réécrire chaque méthode de façon non générique. D'autres idées?C#: Interface générique pour les nombres
Pour référence, voici le code que j'ai d'abord essayé:
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, BYTE_SIZE, 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>(pnaValues, UINT16_SIZE, UINT32_SIZE);
}
...
public static UInt64[] ToUInt64s(UInt32[] pnaValues)
{
return ToLarger<UInt64, UInt32>(pnaValues, 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, UInt16, UInt32, 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, UInt16, UInt32, UInt64
where TLarger : byte, UInt16, UInt32, 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;
}
En particulier dans ce cas, 'Operator.Convert (TFrom value)' peut être utile. –
Notez que je n'ai pas implémenté 'LeftShift' et' RightShift', mais ils seraient triviaux à ajouter. –