unity3d - C#: automatic conversion from enum to class -


consider scenario.

  1. there struct color (written else)
  2. there enum colorcode implements html named color codes.
  3. there's static function converts colorcode color
  4. i want able this:

    color tmp = ....; tmp = colorcode.aqua; 

how do without copy-pasting text 140 times?

i don't care colorcode (enum, class, whatever) long above line works.

problem:

c# not allow me define operators enums. not have macros make nice human-readable table within colorcode.

restriction:

contents of colorcode should available ints, should assignable/ convertible color.


code fragments:

public enum colorcode{     aliceblue = 0xf0f8ff,     antiquewhite = 0xfaebd7,     aqua = 0x00ffff,     aquamarine = 0x7fffd4,     azure = 0xf0ffff,  ///repeat 140 times     ... }  public static color colorfromcode(colorcode code){  .... } 

you write extension method on enum:

public static color tocolor(this colorcode colorcode) {     ... } 

then have:

color tmp = colorcode.aqua.tocolor(); 

it's not quite implicit conversion, it's readable you're get.


Comments