private static bool ValueExists(TEnumeration[] values, TEnum value, int count, EqualityComparer <TEnum> eq) { AZAssert.NotNullInternal(values, nameof(values)); AZAssert.NotNullInternal(eq, nameof(eq)); AZAssert.BoundsInternal(values, count, nameof(count), true); for (int i = 0; i < count; ++i) { if (eq.Equals(values[i].EnumValue, value)) { return(true); } } return(false); }
// lastVal is the instance that is calling InitializeNames (from its ctor). (lastVal == values[values.Length - 1]) private static void InitializeInfoAndNames(TEnumeration lastVal) { AZAssert.NotNullInternal(lastVal, nameof(lastVal)); AZAssert.NotNullInternal(fields, nameof(fields)); string err = null; var t = typeof(TEnumeration); FieldInfo[] fieldArray = fields; for (int i = 0; i < fieldArray.Length; ++i) { var f = fieldArray[i]; if (!f.IsInitOnly || f.FieldType != t) { err = ErrPrefix + "." + f.Name + ERR_FIELD_TYPE; break; } var val = lastVal.GetStaticFieldValue(f); if (val == null) // we expect exactly one field to be null: the last field in textual order (i.e. the field that corresponds to the instance that is calling this method). { val = lastVal; // if this happens more than once it means that not all fields have initializers, but then TryInitializeName will fail below, so it will get caught. } if (!val.TryInitializeName(f.Name)) // TryInitializeName fails if name is already set, and since values.Length == fields.Length this ensures uniqueness! { err = ErrPrefix + "." + f.Name + ERR_FIELD_VALUE; break; } } if (err != null) { Debug.Assert(false, err); throw new Exception(err); } fields = null; // fields array not needed anymore. ev_instance = new EnumValues <TEnumeration>(values); // info init. // late initialization completed successfully. }
// internal instantiation only! internal EnumValues(TEnumeration[] values) { AZAssert.NotNullInternal(values, nameof(values)); this.values = values; }