Can we define implicit conversions of enums in c#?

前端 未结 13 2395
孤街浪徒
孤街浪徒 2020-11-30 18:37

Is it possible to define an implicit conversion of enums in c#?

something that could achieve this?

public enum MyEnum
{
    one = 1, two = 2
}

MyEnu         


        
13条回答
  •  青春惊慌失措
    2020-11-30 18:44

    @BatteryBackupUnit Hey this sounds like a cool solution but could you explain this part here?

    Since im getting with .NET 4.7.2 an "InvalidCastException" out of this sadly :/

     _name = new Lazy(
                    () => EnumInstanceToNameMapping
                         .First(x => ReferenceEquals(this, x.Instance))
                         .Name);
    

    I dont know why, i have created a derived type of the RichEnum and initialized as everything u did in the example but i getthis annyoingg exception..

    Would be glad of some help to this since i like this approach alot tbh.

提交回复
热议问题