mirror of
https://github.com/Ellpeck/DynamicEnums.git
synced 2024-11-25 09:58:35 +01:00
added DynamicallyAccessedMembers attributes
This commit is contained in:
parent
6ab5b2b4a5
commit
1a80ffa622
1 changed files with 69 additions and 13 deletions
|
@ -5,6 +5,10 @@ using System.Linq;
|
|||
using System.Numerics;
|
||||
using System.Reflection;
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endif
|
||||
|
||||
namespace DynamicEnums {
|
||||
/// <summary>
|
||||
/// A dynamic enum is a class that represents enum-like single-instance value behavior with additional capabilities, including dynamic addition of new arbitrary values.
|
||||
|
@ -100,7 +104,11 @@ namespace DynamicEnums {
|
|||
/// <typeparam name="T">The type to add this value to</typeparam>
|
||||
/// <returns>The newly created enum value</returns>
|
||||
/// <exception cref="ArgumentException">Thrown if the name or value passed are already present</exception>
|
||||
public static T Add<T>(string name, BigInteger value) where T : DynamicEnum {
|
||||
public static T Add<
|
||||
#if NET6_0_OR_GREATER
|
||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
|
||||
#endif
|
||||
T>(string name, BigInteger value) where T : DynamicEnum {
|
||||
var storage = DynamicEnum.GetStorage(typeof(T));
|
||||
|
||||
// cached parsed values and names might be incomplete with new values
|
||||
|
@ -126,7 +134,11 @@ namespace DynamicEnums {
|
|||
/// <param name="name">The name of the enum value to add</param>
|
||||
/// <typeparam name="T">The type to add this value to</typeparam>
|
||||
/// <returns>The newly created enum value</returns>
|
||||
public static T AddValue<T>(string name) where T : DynamicEnum {
|
||||
public static T AddValue<
|
||||
#if NET6_0_OR_GREATER
|
||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
|
||||
#endif
|
||||
T>(string name) where T : DynamicEnum {
|
||||
BigInteger value = 0;
|
||||
while (DynamicEnum.IsDefined(typeof(T), value))
|
||||
value++;
|
||||
|
@ -141,7 +153,11 @@ namespace DynamicEnums {
|
|||
/// <param name="name">The name of the enum value to add</param>
|
||||
/// <typeparam name="T">The type to add this value to</typeparam>
|
||||
/// <returns>The newly created enum value</returns>
|
||||
public static T AddFlag<T>(string name) where T : DynamicEnum {
|
||||
public static T AddFlag<
|
||||
#if NET6_0_OR_GREATER
|
||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
|
||||
#endif
|
||||
T>(string name) where T : DynamicEnum {
|
||||
BigInteger value = 1;
|
||||
while (DynamicEnum.IsDefined(typeof(T), value))
|
||||
value <<= 1;
|
||||
|
@ -190,7 +206,11 @@ namespace DynamicEnums {
|
|||
/// <param name="combinedFlag">The combined flags whose individual flags to return.</param>
|
||||
/// <typeparam name="T">The type of enum.</typeparam>
|
||||
/// <returns>All of the unique flags that make up <paramref name="combinedFlag"/>.</returns>
|
||||
public static IEnumerable<T> GetUniqueFlags<T>(T combinedFlag) where T : DynamicEnum {
|
||||
public static IEnumerable<T> GetUniqueFlags<
|
||||
#if NET6_0_OR_GREATER
|
||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
|
||||
#endif
|
||||
T>(T combinedFlag) where T : DynamicEnum {
|
||||
// we can't use the same method here as EnumHelper.GetUniqueFlags since DynamicEnum doesn't guarantee sorted values
|
||||
var max = DynamicEnum.GetValues<T>().Max(DynamicEnum.GetValue);
|
||||
var uniqueFlag = BigInteger.One;
|
||||
|
@ -211,7 +231,11 @@ namespace DynamicEnums {
|
|||
/// <param name="right">The right value</param>
|
||||
/// <typeparam name="T">The type of the values</typeparam>
|
||||
/// <returns>The bitwise OR (|) combination</returns>
|
||||
public static T Or<T>(T left, T right) where T : DynamicEnum {
|
||||
public static T Or<
|
||||
#if NET6_0_OR_GREATER
|
||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
|
||||
#endif
|
||||
T>(T left, T right) where T : DynamicEnum {
|
||||
var cache = DynamicEnum.GetStorage(typeof(T)).OrCache;
|
||||
if (!cache.TryGetValue((left, right), out var ret)) {
|
||||
ret = DynamicEnum.GetEnumValue<T>(DynamicEnum.GetValue(left) | DynamicEnum.GetValue(right));
|
||||
|
@ -227,7 +251,11 @@ namespace DynamicEnums {
|
|||
/// <param name="right">The right value</param>
|
||||
/// <typeparam name="T">The type of the values</typeparam>
|
||||
/// <returns>The bitwise AND (&) combination</returns>
|
||||
public static T And<T>(T left, T right) where T : DynamicEnum {
|
||||
public static T And<
|
||||
#if NET6_0_OR_GREATER
|
||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
|
||||
#endif
|
||||
T>(T left, T right) where T : DynamicEnum {
|
||||
var cache = DynamicEnum.GetStorage(typeof(T)).AndCache;
|
||||
if (!cache.TryGetValue((left, right), out var ret)) {
|
||||
ret = DynamicEnum.GetEnumValue<T>(DynamicEnum.GetValue(left) & DynamicEnum.GetValue(right));
|
||||
|
@ -243,7 +271,11 @@ namespace DynamicEnums {
|
|||
/// <param name="right">The right value</param>
|
||||
/// <typeparam name="T">The type of the values</typeparam>
|
||||
/// <returns>The bitwise XOR (^) combination</returns>
|
||||
public static T Xor<T>(T left, T right) where T : DynamicEnum {
|
||||
public static T Xor<
|
||||
#if NET6_0_OR_GREATER
|
||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
|
||||
#endif
|
||||
T>(T left, T right) where T : DynamicEnum {
|
||||
var cache = DynamicEnum.GetStorage(typeof(T)).XorCache;
|
||||
if (!cache.TryGetValue((left, right), out var ret)) {
|
||||
ret = DynamicEnum.GetEnumValue<T>(DynamicEnum.GetValue(left) ^ DynamicEnum.GetValue(right));
|
||||
|
@ -258,7 +290,11 @@ namespace DynamicEnums {
|
|||
/// <param name="value">The value</param>
|
||||
/// <typeparam name="T">The type of the values</typeparam>
|
||||
/// <returns>The bitwise NEG (~) value</returns>
|
||||
public static T Neg<T>(T value) where T : DynamicEnum {
|
||||
public static T Neg<
|
||||
#if NET6_0_OR_GREATER
|
||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
|
||||
#endif
|
||||
T>(T value) where T : DynamicEnum {
|
||||
var cache = DynamicEnum.GetStorage(typeof(T)).NegCache;
|
||||
if (!cache.TryGetValue(value, out var ret)) {
|
||||
ret = DynamicEnum.GetEnumValue<T>(~DynamicEnum.GetValue(value));
|
||||
|
@ -282,7 +318,11 @@ namespace DynamicEnums {
|
|||
/// <param name="value">The value whose dynamic enum value to get</param>
|
||||
/// <typeparam name="T">The type that the returned dynamic enum should have</typeparam>
|
||||
/// <returns>The defined or combined dynamic enum value</returns>
|
||||
public static T GetEnumValue<T>(BigInteger value) where T : DynamicEnum {
|
||||
public static T GetEnumValue<
|
||||
#if NET6_0_OR_GREATER
|
||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
|
||||
#endif
|
||||
T>(BigInteger value) where T : DynamicEnum {
|
||||
return (T) DynamicEnum.GetEnumValue(typeof(T), value);
|
||||
}
|
||||
|
||||
|
@ -292,7 +332,11 @@ namespace DynamicEnums {
|
|||
/// <param name="type">The type that the returned dynamic enum should have</param>
|
||||
/// <param name="value">The value whose dynamic enum value to get</param>
|
||||
/// <returns>The defined or combined dynamic enum value</returns>
|
||||
public static DynamicEnum GetEnumValue(Type type, BigInteger value) {
|
||||
public static DynamicEnum GetEnumValue(
|
||||
#if NET6_0_OR_GREATER
|
||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
|
||||
#endif
|
||||
Type type, BigInteger value) {
|
||||
var storage = DynamicEnum.GetStorage(type);
|
||||
|
||||
// get the defined value if it exists
|
||||
|
@ -315,7 +359,11 @@ namespace DynamicEnums {
|
|||
/// <param name="strg">The string to parse into a dynamic enum value</param>
|
||||
/// <typeparam name="T">The type of the dynamic enum value to parse</typeparam>
|
||||
/// <returns>The parsed enum value, or null if parsing fails</returns>
|
||||
public static T Parse<T>(string strg) where T : DynamicEnum {
|
||||
public static T Parse<
|
||||
#if NET6_0_OR_GREATER
|
||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
|
||||
#endif
|
||||
T>(string strg) where T : DynamicEnum {
|
||||
return (T) DynamicEnum.Parse(typeof(T), strg);
|
||||
}
|
||||
|
||||
|
@ -327,7 +375,11 @@ namespace DynamicEnums {
|
|||
/// <param name="type">The type of the dynamic enum value to parse</param>
|
||||
/// <param name="strg">The string to parse into a dynamic enum value</param>
|
||||
/// <returns>The parsed enum value, or null if parsing fails</returns>
|
||||
public static DynamicEnum Parse(Type type, string strg) {
|
||||
public static DynamicEnum Parse(
|
||||
#if NET6_0_OR_GREATER
|
||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
|
||||
#endif
|
||||
Type type, string strg) {
|
||||
var cache = DynamicEnum.GetStorage(type).ParseCache;
|
||||
if (!cache.TryGetValue(strg, out var cached)) {
|
||||
BigInteger? accum = null;
|
||||
|
@ -375,7 +427,11 @@ namespace DynamicEnums {
|
|||
return storage;
|
||||
}
|
||||
|
||||
private static DynamicEnum Construct(Type type, string name, BigInteger value) {
|
||||
private static DynamicEnum Construct(
|
||||
#if NET6_0_OR_GREATER
|
||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
|
||||
#endif
|
||||
Type type, string name, BigInteger value) {
|
||||
return (DynamicEnum) Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[] {name, value}, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue