Compare commits

...

5 commits

4 changed files with 66 additions and 21 deletions

View file

@ -4,6 +4,7 @@ using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text;
#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
@ -29,13 +30,22 @@ namespace DynamicEnums {
/// </remarks>
public abstract class DynamicEnum {
private const BindingFlags ConstructorFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
private static readonly Type[] ConstructorTypes = {typeof(string), typeof(BigInteger), typeof(bool)};
private static readonly Dictionary<Type, Storage> Storages = new Dictionary<Type, Storage>();
private readonly BigInteger value;
private Dictionary<DynamicEnum, bool> allFlagsCache;
private Dictionary<DynamicEnum, bool> anyFlagsCache;
private BigInteger value;
private string name;
/// <summary>
/// Creates a new dynamic enum instance.
/// This constructor is protected as it is only invoked via reflection.
/// This constructor is only called if the class doesn't have the <see cref="DynamicEnum(string,BigInteger,bool)"/> constructor.
/// </summary>
protected DynamicEnum() {}
/// <summary>
/// Creates a new dynamic enum instance.
/// This constructor is protected as it is only invoked via reflection.
@ -44,8 +54,8 @@ namespace DynamicEnums {
/// <param name="value">The value</param>
/// <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.name = name;
this.value = value;
}
/// <summary>
@ -84,17 +94,32 @@ namespace DynamicEnums {
/// <summary>Returns a string that represents the current object.</summary>
/// <returns>A string that represents the current object.</returns>
public override string ToString() {
if (this.name == null) {
var included = new List<DynamicEnum>();
if (DynamicEnum.GetValue(this) != 0) {
foreach (var v in DynamicEnum.GetValues(this.GetType())) {
if (this.HasAllFlags(v) && DynamicEnum.GetValue(v) != 0)
included.Add(v);
if (this.name != null)
return this.name;
var storage = DynamicEnum.GetStorage(this.GetType());
if (!storage.CombinedNameCache.TryGetValue(this, out var combinedName)) {
// Enum ToString remarks: https://learn.microsoft.com/en-us/dotnet/api/system.enum.tostring
// If the FlagsAttribute is not applied to this enumerated type and there is a named constant equal to the value of this instance, then the return value is a string containing the name of the constant.
// If the FlagsAttribute is applied and there is a combination of one or more named constants equal to the value of this instance, then the return value is a string containing a delimiter-separated list of the names of the constants.
// Otherwise, the return value is the string representation of the numeric value of this instance.
var included = new StringBuilder();
var remain = DynamicEnum.GetValue(this);
foreach (var other in storage.Values.Values) {
if (this.HasAllFlags(other)) {
var otherValue = DynamicEnum.GetValue(other);
if (otherValue != 0) {
if (included.Length > 0)
included.Append(" | ");
included.Append(other);
remain &= ~otherValue;
}
}
}
this.name = included.Count > 0 ? string.Join(" | ", included) : DynamicEnum.GetValue(this).ToString();
combinedName = included.Length > 0 && remain == 0 ? included.ToString() : DynamicEnum.GetValue(this).ToString();
storage.CombinedNameCache.Add(this, combinedName);
}
return this.name;
return combinedName;
}
/// <summary>
@ -433,7 +458,17 @@ namespace DynamicEnums {
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
#endif
Type type, string name, BigInteger value, bool defined) {
return (DynamicEnum) Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[] {name, value, defined}, CultureInfo.InvariantCulture);
// try to call the constructor with parameters first
var parameterConstructor = type.GetConstructor(DynamicEnum.ConstructorFlags, null, DynamicEnum.ConstructorTypes, null);
if (parameterConstructor != null)
return (DynamicEnum) parameterConstructor.Invoke(DynamicEnum.ConstructorFlags, null, new object[] {name, value, defined}, CultureInfo.InvariantCulture);
// default to the empty constructor and set the values manually
var emptyConstructor = type.GetConstructor(DynamicEnum.ConstructorFlags, null, Type.EmptyTypes, null);
var ret = (DynamicEnum) emptyConstructor.Invoke(DynamicEnum.ConstructorFlags, null, null, CultureInfo.InvariantCulture);
ret.name = name;
ret.value = value;
return ret;
}
private class Storage {
@ -445,6 +480,7 @@ namespace DynamicEnums {
public readonly Dictionary<(DynamicEnum, DynamicEnum), DynamicEnum> AndCache = new Dictionary<(DynamicEnum, DynamicEnum), DynamicEnum>();
public readonly Dictionary<(DynamicEnum, DynamicEnum), DynamicEnum> XorCache = new Dictionary<(DynamicEnum, DynamicEnum), DynamicEnum>();
public readonly Dictionary<DynamicEnum, DynamicEnum> NegCache = new Dictionary<DynamicEnum, DynamicEnum>();
public readonly Dictionary<DynamicEnum, string> CombinedNameCache = new Dictionary<DynamicEnum, string>();
public void ClearCaches() {
this.FlagCache.Clear();
@ -453,6 +489,7 @@ namespace DynamicEnums {
this.AndCache.Clear();
this.XorCache.Clear();
this.NegCache.Clear();
this.CombinedNameCache.Clear();
}
}

View file

@ -13,7 +13,7 @@
<RepositoryUrl>https://github.com/Ellpeck/DynamicEnums</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<VersionPrefix>1.2.0</VersionPrefix>
<VersionPrefix>1.3.0</VersionPrefix>
</PropertyGroup>
<ItemGroup>

View file

@ -19,9 +19,6 @@ public class MyEnum : DynamicEnum {
public static readonly MyEnum FlagTwo = DynamicEnum.AddFlag<MyEnum>("FlagTwo");
public static readonly MyEnum FlagThree = DynamicEnum.AddFlag<MyEnum>("FlagThree");
// this constructor is called internally using reflection
public MyEnum(string name, BigInteger value) : base(name, value) {}
// you can optionally create operator overloads for easier operations
public static implicit operator BigInteger(MyEnum value) => DynamicEnum.GetValue(value);
public static implicit operator MyEnum(BigInteger value) => DynamicEnum.GetEnumValue<MyEnum>(value);

View file

@ -32,8 +32,12 @@ public class EnumTests {
var flags = new TestDynamicEnum[100];
for (var i = 0; i < flags.Length; i++)
flags[i] = DynamicEnum.AddFlag<TestDynamicEnum>("Flag" + i);
var zero = DynamicEnum.Add<TestDynamicEnum>("Zero", 0);
var combined = DynamicEnum.Add<TestDynamicEnum>("Combined", DynamicEnum.GetValue(DynamicEnum.Or(flags[7], flags[13])));
DynamicEnum.Add<TestEnumWithConstructor>("Test", 10);
Assert.AreEqual(DynamicEnum.GetEnumValue<TestEnumWithConstructor>(10).ToString(), "TestModified");
Assert.AreEqual(DynamicEnum.GetValue(flags[7]), BigInteger.One << 7);
Assert.AreEqual(DynamicEnum.GetEnumValue<TestDynamicEnum>(BigInteger.One << 75), flags[75]);
@ -53,8 +57,13 @@ public class EnumTests {
Assert.AreEqual(DynamicEnum.Parse<TestDynamicEnum>("Flag24"), flags[24]);
Assert.AreEqual(DynamicEnum.Parse<TestDynamicEnum>("Flag24 | Flag43"), DynamicEnum.Or(flags[24], flags[43]));
Assert.AreEqual(flags[24].ToString(), "Flag24");
Assert.AreEqual(zero.ToString(), "Zero");
Assert.AreEqual(DynamicEnum.GetEnumValue<TestDynamicEnum>(0).ToString(), "Zero");
Assert.AreEqual(DynamicEnum.Or(flags[24], flags[43]).ToString(), "Flag24 | Flag43");
Assert.AreEqual(DynamicEnum.Or(flags[24], DynamicEnum.GetEnumValue<TestDynamicEnum>(new BigInteger(1) << 99)).ToString(), "Flag24 | Flag99");
Assert.AreEqual(DynamicEnum.Or(flags[24], DynamicEnum.GetEnumValue<TestDynamicEnum>(new BigInteger(1) << 101)).ToString(), (DynamicEnum.GetValue(flags[24]) | new BigInteger(1) << 101).ToString());
Assert.True(DynamicEnum.IsDefined(flags[27]));
Assert.True(DynamicEnum.IsDefined(combined));
@ -63,7 +72,7 @@ public class EnumTests {
Assert.AreEqual(
new[] {flags[0], flags[7], flags[13], combined},
DynamicEnum.GetFlags(DynamicEnum.Or(DynamicEnum.Or(flags[0], flags[13]), flags[7])));
DynamicEnum.GetFlags(DynamicEnum.Or(DynamicEnum.Or(flags[0], flags[13]), flags[7]), false));
Assert.AreEqual(
new[] {flags[0], flags[7], flags[13]},
@ -84,9 +93,11 @@ public class EnumTests {
}
private class TestDynamicEnum : DynamicEnum {
private class TestDynamicEnum : DynamicEnum;
public TestDynamicEnum(string name, BigInteger value, bool defined) : base(name, value, defined) {}
private class TestEnumWithConstructor : DynamicEnum {
public TestEnumWithConstructor(string name, BigInteger value, bool defined) : base($"{name}Modified", value, defined) {}
}