c# - Generate JavaScript Representation of Enums -


i have few [flags] enums in code i'd expose javascript without copy&pasting. signalr seems doing similar hub-proxies mapping url action returning javascript stubs generated reflection. since code generated @ runtime, doesn't seem possible included bundles.

as alternative, implemented t4 template generate js file @ design time:

<#@ template debug="false" hostspecific="true" language="c#" #> <#@ assembly name="system.core" #> <#@ assembly name="envdte" #> <#@ import namespace="envdte" #> <#@ import namespace="system.linq" #> <#@ import namespace="system.text" #> <#@ import namespace="system.collections.generic" #> <#@ output extension=".js" #> enums = { <#     var visualstudio = (host iserviceprovider).getservice(typeof(envdte.dte))                         envdte.dte;     var project = visualstudio.solution.findprojectitem(this.host.templatefile)                                         .containingproject envdte.project;      foreach(envdte.projectitem item in getprojectitemsrecursively(project.projectitems))     {         if (item.filecodemodel == null) continue;         foreach(envdte.codeelement elem in item.filecodemodel.codeelements)         {             if (elem.kind == envdte.vscmelement.vscmelementnamespace)             {                 foreach (codeelement innerelement in elem.children)                 {                     if (innerelement.kind == vscmelement.vscmelementenum)                     {                         codeenum enu = (codeenum)innerelement; #>  <#= enu.name #>: { <#         dictionary<string, string> values = new dictionary<string, string>();         foreach (codeelement child in enu.members)         {             codevariable value = child codevariable;              if (value != null) {                 string init = value.initexpression string;                 int unused;                 if (!int.tryparse(init, out unused))                 {                     foreach (keyvaluepair<string, string> entry in values) {                         init = init.replace(entry.key, entry.value);                     }                     init = "(" + init + ")";                  }                 values.add(value.name, init);                 writeline("\t\t" + value.name + ": " + init + ",");             }         } #>     }, <#                     }                 }             }         }     } #> }; <#+   public list<envdte.projectitem> getprojectitemsrecursively(envdte.projectitems items)   {       var ret = new list<envdte.projectitem>();       if (items == null) return ret;       foreach(envdte.projectitem item in items)       {         ret.add(item);         ret.addrange(getprojectitemsrecursively(item.projectitems));       }       return ret;   } #> 

however feels fragile envdte. logic handle enums like:

[flags] public enum access {     none = 0,     read = 1,     write = 2,      readwrite = read | write } 

with composite values dirty hack string replacements. t4 template above generate:

enums = {     access: {         none: 0,         read: 1,         write: 2,         readwrite: (1 | 2),     }, }; 

is there cleaner way achieve this? ideally kind of design-time reflection generate js file available bundling.

i think can use bundle transforms accomplish this...

http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

you can define bundle , attach ibundletransform class interpolates content of source files. should able use reflection write javascript output stream.

if wanted in hacky way easy do. feed empty file , hard-code class use reflection write javascript want.

now, if want design in way don't need modify ibundletransform class in order add additional enums javascript output, you'd need put work real structure. example, let's have enums in file called enums.cs, add file include list of bundle , dynamically parse enum declarations , use reflection find them name in order output each 1 in resulting javascript file.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -