convert an enum to another type of enum

前端 未结 15 1552
感动是毒
感动是毒 2020-11-30 02:36

I have an enum of for example \'Gender\' (Male =0 , Female =1) and I have another enum from a service which has its own Gender enum (Male =0

15条回答
  •  -上瘾入骨i
    2020-11-30 03:33

    Given Enum1 value = ..., then if you mean by name:

    Enum2 value2 = (Enum2) Enum.Parse(typeof(Enum2), value.ToString());
    

    If you mean by numeric value, you can usually just cast:

    Enum2 value2 = (Enum2)value;
    

    (with the cast, you might want to use Enum.IsDefined to check for valid values, though)

提交回复
热议问题