Well, itβs possible, but on the other hand you will need to specify what the base type is:
public static T GetId<T>(this Enum value)
where T : struct, IComparable, IFormattable, IConvertible
{
return (T)Convert.ChangeType(value, typeof(T));
}
System.Enum . :
PersonName person = PersonName.Robert;
short personId = person.GetId<short>();
- long, :
public static long GetId(this Enum value)
{
return Convert.ToInt64(value);
}
:
PersonName person = PersonName.Robert;
long personId = person.GetId();
object, :
public static object GetId(this Enum value)
{
return Convert.ChangeType(task, Enum.GetUnderlyingType(value.GetType()));
}
PersonName person = PersonName.Robert;
var personId = (short)person.GetId();
- . ( int, ), .