TylerBrinkley/Enums.NET

Convert between Enums by Name

Genmutant opened this issue · 2 comments

I tried to write a simple converter between two very similar enums (partially auto generated ones) based on the name, but got stuck quite a bit. Looks like I would need to reimplement a lot of your code to get it to work.
Here is what I wanted to build (without the constraints):

public static TEnumResult ConvertByName<TEnum, TEnumResult>(TEnum e, TEnumResult defaultValue)
{
    TEnumResult result;
    if (!Enums.TryParse(e.AsString(EnumFormat.Name), true, out result))
    {
        return defaultValue;
    }
    return result;
}

Use EnumsNET.Unsafe.UnsafeEnums instead.

Works great, thanks! Hadn't seen the Unsafe package so far.