EnumConverter<TInput, TOutput>
static class EnumConverter<TInput, TOutput> where TInput : struct, IConvertible, IComparable, IFormattable where TOutput : struct, IConvertible, IComparable, IFormattable
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace DotNext
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal static class EnumConverter<[System.Runtime.CompilerServices.Nullable(0)] TInput, [System.Runtime.CompilerServices.Nullable(0)] TOutput> where TInput : struct, IConvertible, IComparable, IFormattable where TOutput : struct, IConvertible, IComparable, IFormattable
{
[System.Runtime.CompilerServices.Nullable(0)]
private static readonly IntPtr Converter;
unsafe static EnumConverter()
{
string text;
switch (Type.GetTypeCode(typeof(TOutput))) {
case TypeCode.Byte:
text = "ToByte";
break;
case TypeCode.SByte:
text = "ToSByte";
break;
case TypeCode.Int16:
text = "ToInt16";
break;
case TypeCode.UInt16:
text = "ToUInt16";
break;
case TypeCode.Int32:
text = "ToInt32";
break;
case TypeCode.UInt32:
text = "ToUInt32";
break;
case TypeCode.Int64:
text = "ToInt64";
break;
case TypeCode.UInt64:
text = "ToUInt64";
break;
case TypeCode.Boolean:
text = "ToBoolean";
break;
case TypeCode.Single:
text = "ToSingle";
break;
case TypeCode.Double:
text = "ToDouble";
break;
case TypeCode.Char:
text = "ToChar";
break;
case TypeCode.Decimal:
text = "ToDecimal";
break;
case TypeCode.DateTime:
text = "ToDateTime";
break;
default:
text = "<unknown>";
break;
}
string name = text;
Type type = typeof(TInput);
if (type.IsEnum)
type = type.GetEnumUnderlyingType();
MethodInfo method = typeof(Convert).GetMethod(name, new Type[1] {
type
});
if ((object)method == null)
Converter = (IntPtr)(void*);
else
Converter = (IntPtr)(void*)method.MethodHandle.GetFunctionPointer();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[System.Runtime.CompilerServices.NullableContext(0)]
internal static TOutput Convert(TInput value)
{
if (Unsafe.SizeOf<TInput>() != Unsafe.SizeOf<TOutput>()) {
IntPtr converter = Converter;
return (TOutput);
}
return Unsafe.As<TInput, TOutput>(ref value);
}
}
}