// TEnumX must either be the System.Enum class or an EnumCompatible primitive type (the latter of which includes user defined System.Enums). internal virtual bool TryGetEnumValueInternal <TEnumX>(out TEnumX enumValue, bool allowConversion) where TEnumX : IConvertible { AZAssert.Internal(Meta.Evaluate.IsEnumCompatible <TEnumX>(), "not enum compatible"); enumValue = default(TEnumX); return(false); }
/// <summary> /// Tries to retrieve the System.Enum value paired to this Enumeration value. /// </summary><returns> /// The System.Enum value paired to this Enumeration value; /// or null if this Enumeration value isn't paired to any System.Enum value. /// </returns> public System.Enum TryGetEnumValue() { if (!HasEnumValueImpl) { return(null); } System.Enum enumValue; TryGetEnumValueInternal(out enumValue, false); AZAssert.Internal(enumValue != null, "failed to get enum value that should exists"); return(enumValue); }
// Enum.TryParse doesn't exist in .net 3.5 :( private static void InitializeEnumValues() { AZAssert.Internal(_IsInitialized, "names not initialized"); bool isOrdinalPaired = true; var eq = eqcTEnum; TEnumeration v = null; try { var tEnum = typeof(TEnum); var values = ValArrayInternal; for (int i = 0; i < values.Length; ++i) { v = values[i]; v.EnumValue = (TEnum)Enum.Parse(tEnum, v.Name); // Verify requirement: Enum value uniqueness if (ValueExists(values, v.EnumValue, i, eq)) { Debug.Assert(false, ErrPrefix + "." + v.Name + ERR_ENUM_UNIQUE); throw new Exception(ErrPrefix + "." + v.Name + ERR_ENUM_UNIQUE); } isOrdinalPaired &= (ulong)i == ToUInt64(v.EnumValue); // i == Ordinal } } catch (ArgumentException e) { Debug.Assert(false, ErrPrefix + v.Name + ERR_ENUM_MISSING, e.Message); throw new Exception(ErrPrefix + v.Name + ERR_ENUM_MISSING, e); } catch (Exception e) { throw new Exception(ErrPrefix + ERR_BASE, e); // This should never happen! } if (isOrdinalPaired) { eqcTEnum = null; } }
// TEnumX must either be System.Enum or a valueType. internal sealed override bool TryGetEnumValueInternal <TEnumX>(out TEnumX enumValue, bool allowConversion) // (TEnumX : IConvertible) { if (EnumValue is TEnumX) // the System.Enum case will fall in here: { enumValue = (TEnumX)(object)EnumValue; // not sure how to get around this ugly double-cast... System.Enum is a bastard! :( return(true); } else { AZAssert.Internal(Evaluate.IsEnumCompatible <TEnumX>(), "not enum compatible"); if (allowConversion) { // we *might* be able to perform some conversion magic even if the type is wrong ;) return(Numeric.TryConvertInteger(EnumValue, out enumValue)); } else { enumValue = default(TEnumX); return(false); } } }