Part of DKNet.Fw.Extensions. Use TryConvertToEnum when a boxed numeric value must become a runtime or generic enum value.
using System;
using DKNet.Fw.Extensions.Reflection;
object raw = 1;
if (raw.TryConvertToEnum<State>(out var state))
Console.WriteLine(state); // Ready
Console.WriteLine(typeof(State).TryConvertToEnum("bad", out _)); // False
public enum State { Unknown, Ready }
The runtime overload accepts Type (including nullable enum types), an input object, and an out object?. The generic overload uses where TEnum : struct, Enum and returns out TEnum?. Conversion uses InvariantCulture; InvalidCastException, FormatException and OverflowException become false with a null result.
ArgumentException when the destination is not an enum."Ready" is not parsed as an enum name; numeric strings can convert. Use Enum.TryParse for named input.