Is there a way to make ToEnum generic

Posted by maxfridbe on Stack Overflow See other posts from Stack Overflow or by maxfridbe
Published on 2010-03-25T20:13:29Z Indexed on 2010/03/25 20:23 UTC
Read the original article Hit count: 334

Filed under:
|
|

I would like to do this but it does not work.

bool TryGetEnum<TEnum, TValue>(TValue value, out TEnum myEnum)
{
    if (Enum.IsDefined(typeof(TEnum), value))
    {
        myEnum = (TEnum)value;
        return true;
    }
    return false;
}

Example usage:

MyEnum mye;
bool success = this.TryGetEnum<MyEnum,char>('c',out mye);

© Stack Overflow or respective owner

Related posts about c#

Related posts about enums