Enum<TEnum>
public struct Enum<TEnum> : IEquatable<TEnum>, IComparable<TEnum>, IFormattable, IEquatable<Enum<TEnum>>, ISerializable, IConvertible<TEnum> where TEnum : struct, Enum
Provides strongly typed way to reflect enum type.
using DotNext.Runtime;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
namespace DotNext
{
[Serializable]
public readonly struct Enum<TEnum> : IEquatable<TEnum>, IComparable<TEnum>, IFormattable, IEquatable<Enum<TEnum>>, ISerializable, IConvertible<TEnum> where TEnum : struct, Enum
{
private readonly struct Tuple : IEquatable<Tuple>
{
[System.Runtime.CompilerServices.Nullable(2)]
internal readonly string Name;
internal readonly TEnum Value;
[System.Runtime.CompilerServices.NullableContext(1)]
private Tuple(string name)
{
Name = name;
Value = default(TEnum);
}
private Tuple(TEnum value)
{
Value = value;
Name = null;
}
public static implicit operator Tuple([System.Runtime.CompilerServices.Nullable(1)] string name)
{
return new Tuple(name);
}
public static implicit operator Tuple(TEnum value)
{
return new Tuple(value);
}
public bool Equals(Tuple other)
{
if (Name != null)
return Name == other.Name;
if (other.Name == null)
return EqualityComparer<TEnum>.Default.Equals(Value, other.Value);
return false;
}
[System.Runtime.CompilerServices.NullableContext(2)]
public override bool Equals(object other)
{
if (other is Tuple) {
Tuple other2 = (Tuple)other;
return Equals(other2);
}
return false;
}
public override int GetHashCode()
{
if (Name != null)
return Name.GetHashCode(StringComparison.Ordinal);
return Value.GetHashCode();
}
}
private sealed class Mapping : Dictionary<Tuple, long>
{
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0
})]
internal readonly Enum<TEnum>[] Members;
internal Enum<TEnum> this[[System.Runtime.CompilerServices.Nullable(1)] string name] {
get {
return Members[base[name]];
}
}
internal Mapping(out Enum<TEnum> min, out Enum<TEnum> max)
{
string[] names = Enum.GetNames(typeof(TEnum));
TEnum[] array = (TEnum[])Enum.GetValues(typeof(TEnum));
Members = new Enum<TEnum>[names.LongLength];
min = (max = default(Enum<TEnum>));
for (long num = 0; num < names.LongLength; num++) {
Enum<TEnum> enum = Members[num] = new Enum<TEnum>(array[num], names[num]);
Add(enum.Name, num);
base[enum.Value] = num;
TEnum value = enum.Value;
min = ((value.CompareTo(min.Value) < 0) ? enum : min);
value = enum.Value;
max = ((value.CompareTo(max.Value) > 0) ? enum : max);
}
}
internal bool TryGetValue(TEnum value, out Enum<TEnum> member)
{
if (TryGetValue(value, out long value2)) {
member = Members[value2];
return true;
}
member = default(Enum<TEnum>);
return false;
}
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})]
private static readonly Mapping EnumMapping = new Mapping(out MinValue, out MaxValue);
public static readonly Enum<TEnum> MaxValue;
public static readonly Enum<TEnum> MinValue;
[System.Runtime.CompilerServices.Nullable(1)]
private const string NameSerData = "Name";
[System.Runtime.CompilerServices.Nullable(1)]
private const string ValueSerData = "Value";
[System.Runtime.CompilerServices.Nullable(2)]
private readonly string name;
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0
})]
public static IReadOnlyList<Enum<TEnum>> Members {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
0
})]
get {
return EnumMapping.Members;
}
}
[System.Runtime.CompilerServices.Nullable(1)]
public static Type UnderlyingType {
[System.Runtime.CompilerServices.NullableContext(1)]
get {
return Enum.GetUnderlyingType(typeof(TEnum));
}
}
public static TypeCode UnderlyingTypeCode => Type.GetTypeCode(typeof(TEnum));
public TEnum Value { get; }
[System.Runtime.CompilerServices.Nullable(1)]
public string Name {
[System.Runtime.CompilerServices.NullableContext(1)]
get {
return name ?? ValueTypeExtensions.ToString<TEnum>(Value, (IFormatProvider)null);
}
}
public static bool IsDefined(TEnum value)
{
return EnumMapping.ContainsKey(value);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static bool IsDefined(string name)
{
return EnumMapping.ContainsKey(name);
}
public static Enum<TEnum> GetMember(TEnum value)
{
if (!EnumMapping.TryGetValue(value, out Enum<TEnum> member))
return new Enum<TEnum>(value, null);
return member;
}
public static bool TryGetMember(TEnum value, out Enum<TEnum> member)
{
return EnumMapping.TryGetValue(value, out member);
}
public static bool TryGetMember([System.Runtime.CompilerServices.Nullable(1)] string name, out Enum<TEnum> member)
{
if (Enum.TryParse<TEnum>(name, out TEnum result)) {
member = new Enum<TEnum>(result, name);
return true;
}
member = default(Enum<TEnum>);
return false;
}
public static Enum<TEnum> GetMember([System.Runtime.CompilerServices.Nullable(1)] string name)
{
return EnumMapping[name];
}
private Enum(TEnum value, [System.Runtime.CompilerServices.Nullable(2)] string name)
{
Value = value;
this.name = name;
}
[System.Runtime.CompilerServices.NullableContext(1)]
private Enum(SerializationInfo info, StreamingContext context)
{
name = info.GetString("Name");
Value = (TEnum)info.GetValue("Value", typeof(TEnum));
}
public bool IsFlag(TEnum flags)
{
return Intrinsics.HasFlag<TEnum>(flags, Value);
}
TEnum IConvertible<TEnum>.Convert()
{
return Value;
}
public static implicit operator TEnum([In] [IsReadOnly] ref Enum<TEnum> en)
{
return en.Value;
}
public int CompareTo(TEnum other)
{
return Comparer<TEnum>.Default.Compare(Value, other);
}
public bool Equals(TEnum other)
{
return EqualityComparer<TEnum>.Default.Equals(Value, other);
}
public bool Equals(Enum<TEnum> other)
{
if (Equals(other.Value))
return object.Equals(Name, other.Name);
return false;
}
[System.Runtime.CompilerServices.NullableContext(2)]
public override bool Equals(object other)
{
if (!(other is Enum<TEnum>)) {
if (!(other is TEnum))
return false;
TEnum other2 = (TEnum)other;
return Equals(other2);
}
Enum<TEnum> other3 = (Enum<TEnum>)other;
return Equals(other3);
}
public override int GetHashCode()
{
return HashCode.Combine<TEnum, string>(Value, Name);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public override string ToString()
{
return ValueTypeExtensions.ToString<TEnum>(Value, (IFormatProvider)null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
string IFormattable.ToString(string format, IFormatProvider provider)
{
return ValueTypeExtensions.ToString<TEnum>(Value, format, provider);
}
public static bool operator ==(Enum<TEnum> first, Enum<TEnum> second)
{
return first.Equals(second);
}
public static bool operator !=(Enum<TEnum> first, Enum<TEnum> second)
{
return !first.Equals(second);
}
[System.Runtime.CompilerServices.NullableContext(1)]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Name", name, typeof(string));
info.AddValue("Value", Value);
}
}
}