c# 避免box、unbox
1、byte、int为key
2、dictionnary
使用enum、定义自定义的struct时,实现IEquatable <T>接口
private static EqualityComparer<T> CreateComparer() { RuntimeType runtimeType = (RuntimeType)typeof(T); if ((Type)runtimeType == typeof(byte)) { return (EqualityComparer<T>)new ByteEqualityComparer(); } if (typeof(IEquatable<T>).IsAssignableFrom(runtimeType)) { return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(GenericEqualityComparer<int>), runtimeType); } if (runtimeType.IsGenericType && runtimeType.GetGenericTypeDefinition() == typeof(Nullable<>)) { RuntimeType runtimeType2 = (RuntimeType)runtimeType.GetGenericArguments()[0]; if (typeof(IEquatable<>).MakeGenericType(runtimeType2).IsAssignableFrom(runtimeType2)) { return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(NullableEqualityComparer<int>), runtimeType2); } } if (runtimeType.IsEnum) { switch (Type.GetTypeCode(Enum.GetUnderlyingType(runtimeType))) { case TypeCode.Int16: return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(ShortEnumEqualityComparer<short>), runtimeType); case TypeCode.SByte: return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(SByteEnumEqualityComparer<sbyte>), runtimeType); case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(EnumEqualityComparer<int>), runtimeType); case TypeCode.Int64: case TypeCode.UInt64: return (EqualityComparer<T>)RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter((RuntimeType)typeof(LongEnumEqualityComparer<long>), runtimeType); } } return new ObjectEqualityComparer<T>(); } internal class ObjectEqualityComparer<T> : EqualityComparer<T> { public override bool Equals(T x, T y) { if (x != null) { if (y != null) { return x.Equals(y); } return false; } if (y != null) { return false; } return true; } public override int GetHashCode(T obj) { return obj?.GetHashCode() ?? 0; } internal override int IndexOf(T[] array, T value, int startIndex, int count) { int num = startIndex + count; if (value == null) { for (int i = startIndex; i < num; i++) { if (array[i] == null) { return i; } } } else { for (int j = startIndex; j < num; j++) { if (array[j] != null && array[j].Equals(value)) { return j; } } } return -1; } internal override int LastIndexOf(T[] array, T value, int startIndex, int count) { int num = startIndex - count + 1; if (value == null) { for (int num2 = startIndex; num2 >= num; num2--) { if (array[num2] == null) { return num2; } } } else { for (int num3 = startIndex; num3 >= num; num3--) { if (array[num3] != null && array[num3].Equals(value)) { return num3; } } } return -1; } public override bool Equals(object obj) { ObjectEqualityComparer<T> objectEqualityComparer = obj as ObjectEqualityComparer<T>; return objectEqualityComparer != null; } public override int GetHashCode() { return base.GetType().Name.GetHashCode(); } }

浙公网安备 33010602011771号