0

I'd like to generate dictionary lookup code snippet that iterate through existing enums and does a foreach enum print "{ enum.A, "A"},"

Like how the code snippet does 'switch', but my own version of it.

I'm currently using "Snippet Designer" for convenience.

I currently have the following

IDictionary<$key$, $value$> _lookup = new Dictionary<$key$, $value$>() 
{
    { $key$, $value$ },
};

$end$

Edit: Expected result (after filling out the code snippet in the IDE)

enum FakeEnum
    {
        Foo, Blah, Bar
    }

    IDictionary<FakeEnum, string> _lookup = new Dictionary<FakeEnum, string>() 
    { 
        {FakeEnum.Foo, string}, 
        {FakeEnum.Blah, string}, 
        {FakeEnum.Bar, string} 
    } ;
PanMan
  • 1
  • 2

1 Answers1

0
IDictionary<$enum$, string> _lookup = new Dictionary<$enum$, string>();

foreach( $enum$ value in Enum.GetValues(typeof($enum$)) )
{
    _lookup .Add(value, value.ToString());
}
$end$

(based on Can you loop through all enum values?)


or try this:

var _lookup = Enum.GetValues(typeof($enum$))
               .Cast<$enum$>()
               .ToDictionary(t => t, t => t.ToString() );
$end$

based on Enum to Dictionary c#

Community
  • 1
  • 1
blueberryfields
  • 45,910
  • 28
  • 89
  • 168
  • I'm unsure if you're getting this confused with actual code or not. This code is built for code snippet generation. EG. typing prop, in the IDE will bring up a snippet for you to template your own values into – PanMan Jun 18 '14 at 04:07