pass defined as a constructor argument

This commit is contained in:
Ell 2024-02-04 11:09:13 +01:00
parent 5146f7d8a8
commit ffbd154153
2 changed files with 8 additions and 7 deletions

View file

@ -42,7 +42,8 @@ namespace DynamicEnums {
/// </summary> /// </summary>
/// <param name="name">The name of the enum value</param> /// <param name="name">The name of the enum value</param>
/// <param name="value">The value</param> /// <param name="value">The value</param>
protected DynamicEnum(string name, BigInteger value) { /// <param name="defined">Whether this enum value <see cref="IsDefined(DynamicEnum)"/>, and thus, not a combined flag.</param>
protected DynamicEnum(string name, BigInteger value, bool defined) {
this.value = value; this.value = value;
this.name = name; this.name = name;
} }
@ -121,7 +122,7 @@ namespace DynamicEnums {
throw new ArgumentException($"Duplicate name {name}", nameof(name)); throw new ArgumentException($"Duplicate name {name}", nameof(name));
} }
var ret = DynamicEnum.Construct(typeof(T), name, value); var ret = DynamicEnum.Construct(typeof(T), name, value, true);
storage.Values.Add(value, ret); storage.Values.Add(value, ret);
return (T) ret; return (T) ret;
} }
@ -345,7 +346,7 @@ namespace DynamicEnums {
// otherwise, cache the combined value // otherwise, cache the combined value
if (!storage.FlagCache.TryGetValue(value, out var combined)) { if (!storage.FlagCache.TryGetValue(value, out var combined)) {
combined = DynamicEnum.Construct(type, null, value); combined = DynamicEnum.Construct(type, null, value, false);
storage.FlagCache.Add(value, combined); storage.FlagCache.Add(value, combined);
} }
return combined; return combined;
@ -431,8 +432,8 @@ namespace DynamicEnums {
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
#endif #endif
Type type, string name, BigInteger value) { Type type, string name, BigInteger value, bool defined) {
return (DynamicEnum) Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[] {name, value}, CultureInfo.InvariantCulture); return (DynamicEnum) Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[] {name, value, defined}, CultureInfo.InvariantCulture);
} }
private class Storage { private class Storage {

View file

@ -86,7 +86,7 @@ public class EnumTests {
private class TestDynamicEnum : DynamicEnum { private class TestDynamicEnum : DynamicEnum {
public TestDynamicEnum(string name, BigInteger value) : base(name, value) {} public TestDynamicEnum(string name, BigInteger value, bool defined) : base(name, value, defined) {}
} }