Пример #1
0
        internal static NonGenericEnumInfo GetNonGenericEnumInfo(Type enumType)
        {
            Preconditions.NotNull(enumType, nameof(enumType));

            var nonGenericEnumInfos = _nonGenericEnumInfos;
            NonGenericEnumInfo info;
            if (!nonGenericEnumInfos.TryGetValue(enumType, out info))
            {
                if (enumType.IsEnum())
                {
                    var closedEnumsType = typeof(Enums<>).MakeGenericType(enumType);
                    info = new NonGenericEnumInfo((IEnumInfo)closedEnumsType.
#if TYPE_REFLECTION
                        GetField("Info", BindingFlags.Static | BindingFlags.Public)
#else
                        GetTypeInfo().GetDeclaredField("Info")
#endif
                    .GetValue(null), false);
                }
                else
                {
                    var nonNullableEnumType = Nullable.GetUnderlyingType(enumType);
                    if (nonNullableEnumType?.IsEnum() != true)
                    {
                        throw new ArgumentException("must be an enum type", nameof(enumType));
                    }
                    info = new NonGenericEnumInfo(GetInfo(nonNullableEnumType), true);
                }
                Dictionary<Type, NonGenericEnumInfo> oldNonGenericEnumInfos;
                do
                {
                    NonGenericEnumInfo foundInfo;
                    if (nonGenericEnumInfos.TryGetValue(enumType, out foundInfo))
                    {
                        return foundInfo;
                    }
                    oldNonGenericEnumInfos = nonGenericEnumInfos;
                    nonGenericEnumInfos = new Dictionary<Type, NonGenericEnumInfo>(nonGenericEnumInfos);
                    nonGenericEnumInfos.Add(enumType, info);
                } while ((nonGenericEnumInfos = Interlocked.CompareExchange(ref _nonGenericEnumInfos, nonGenericEnumInfos, oldNonGenericEnumInfos)) != oldNonGenericEnumInfos);
            }
            return info;
        }
Пример #2
0
        internal static bool EqualsInternal(NonGenericEnumInfo info, object value, object other)
        {
            var enumInfo = info.EnumInfo;

            if (info.IsNullable)
            {
                if (value == null)
                {
                    if (other == null)
                    {
                        return true;
                    }
                    enumInfo.ToObject(other);
                    return false;
                }
                if (other == null)
                {
                    enumInfo.ToObject(value);
                    return false;
                }
            }

            return enumInfo.Equals(value, other);
        }
Пример #3
0
        internal static int CompareToInternal(NonGenericEnumInfo info, object value, object other)
        {
            var enumInfo = info.EnumInfo;
            if (info.IsNullable)
            {
                if (value == null)
                {
                    if (other == null)
                    {
                        return 0;
                    }
                    enumInfo.ToObject(other);
                    return -1;
                }
                if (other == null)
                {
                    enumInfo.ToObject(value);
                    return 1;
                }
            }

            return enumInfo.CompareTo(value, other);
        }
Пример #4
0
 /// <summary>
 /// The <see cref="NonGenericEnumComparer"/> constructor.
 /// </summary>
 /// <param name="enumType">The enum type.</param>
 /// <exception cref="ArgumentNullException"><paramref name="enumType"/> is <c>null</c>.</exception>
 /// <exception cref="ArgumentException"><paramref name="enumType"/> is not an enum type.</exception>
 public NonGenericEnumComparer(Type enumType)
 {
     _info = NonGenericEnums.GetNonGenericEnumInfo(enumType);
 }