Пример #1
0
            static Cache()
            {
                Type           = typeof(T);
                UnderlyingType = Enum.GetUnderlyingType(Type);
                Values         = (Enum.GetValues(Type) as T[]).AsReadOnly();
                Names          = Enum.GetNames(Type).Select(string.Intern).ToReadOnlyArray();
                Members        = Names.Select(x => new Member <T>(x)).ToReadOnlyArray();
                MinValue       = Values.DefaultIfEmpty().Min();
                MaxValue       = Values.DefaultIfEmpty().Max();
                IsEmpty        = Values.Count == 0;
                IsFlags        = Attribute.IsDefined(Type, typeof(FlagsAttribute));
                var distinctedMember = Members.OrderBy(x => x.Value).Distinct(new Member <T> .ValueComparer()).ToArray();

                MemberByName = Members.ToFrozenStringKeyDictionary(x => x.Name);
                UnderlyingOperation
                    = Type.GetTypeCode(Type) switch
                    {
                    TypeCode.SByte => SByteOperation <T> .Create(MinValue, MaxValue, distinctedMember),
                    TypeCode.Byte => ByteOperation <T> .Create(MinValue, MaxValue, distinctedMember),
                    TypeCode.Int16 => Int16Operation <T> .Create(MinValue, MaxValue, distinctedMember),
                    TypeCode.UInt16 => UInt16Operation <T> .Create(MinValue, MaxValue, distinctedMember),
                    TypeCode.Int32 => Int32Operation <T> .Create(MinValue, MaxValue, distinctedMember),
                    TypeCode.UInt32 => UInt32Operation <T> .Create(MinValue, MaxValue, distinctedMember),
                    TypeCode.Int64 => Int64Operation <T> .Create(MinValue, MaxValue, distinctedMember),
                    TypeCode.UInt64 => UInt64Operation <T> .Create(MinValue, MaxValue, distinctedMember),
                    _ => throw new InvalidOperationException(),
                    };
            }
Пример #2
0
            static Cache_UnderlyingOperation()
            {
                var type       = Cache_Type <T> .Type;
                var min        = Cache_MinMaxValues <T> .MinValue;
                var max        = Cache_MinMaxValues <T> .MaxValue;
                var distincted = Cache_Members <T> .Members.OrderBy(x => x.Value).Distinct(new Member <T> .ValueComparer()).ToArray();

                UnderlyingType = Cache_Type <T> .UnderlyingType;
                UnderlyingOperation
                    = Type.GetTypeCode(type) switch
                    {
                    TypeCode.SByte => SByteOperation <T> .Create(min, max, distincted),
                    TypeCode.Byte => ByteOperation <T> .Create(min, max, distincted),
                    TypeCode.Int16 => Int16Operation <T> .Create(min, max, distincted),
                    TypeCode.UInt16 => UInt16Operation <T> .Create(min, max, distincted),
                    TypeCode.Int32 => Int32Operation <T> .Create(min, max, distincted),
                    TypeCode.UInt32 => UInt32Operation <T> .Create(min, max, distincted),
                    TypeCode.Int64 => Int64Operation <T> .Create(min, max, distincted),
                    TypeCode.UInt64 => UInt64Operation <T> .Create(min, max, distincted),
                    _ => throw new InvalidOperationException(),
                    };
            }
Пример #3
0
            static CacheUnderlyingOperation()
            {
                Type type = CacheType <T> .Type;
                T    min  = CacheMinMaxValues <T> .MinValue;
                T    max  = CacheMinMaxValues <T> .MaxValue;

                EnumMember <T>[] distincted = CacheMembers <T> .Members.OrderBy(x => x.Value).Distinct(new EnumMember <T> .ValueComparer()).ToArray();

                UnderlyingType = CacheType <T> .UnderlyingType;
                // ReSharper disable once SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault
                UnderlyingOperation = Type.GetTypeCode(type) switch
                {
                    TypeCode.SByte => SByteOperation <T> .Create(min, max, distincted),
                    TypeCode.Byte => ByteOperation <T> .Create(min, max, distincted),
                    TypeCode.Int16 => Int16Operation <T> .Create(min, max, distincted),
                    TypeCode.UInt16 => UInt16Operation <T> .Create(min, max, distincted),
                    TypeCode.Int32 => Int32Operation <T> .Create(min, max, distincted),
                    TypeCode.UInt32 => UInt32Operation <T> .Create(min, max, distincted),
                    TypeCode.Int64 => Int64Operation <T> .Create(min, max, distincted),
                    TypeCode.UInt64 => UInt64Operation <T> .Create(min, max, distincted),
                    _ => throw new InvalidOperationException()
                };
            }
        }
Пример #4
0
 static UnderlyingTypeInfo()
 {
     UnderlyingType     = Enum.GetUnderlyingType(typeof(T));
     UnderlyingTypeCode = Type.GetTypeCode(UnderlyingType);
     Operation          = GetOperation();
 }