c# - Can I use roslyn and the new .net project format to create classes on build? -
i have project using new .net project format (dnx
, project.json
, using dnvm
mono-1.0.0-beta4, want use coreclr once dnu restore
working on *nix).
one of common problems have .net/c# desire create code @ compile time. example, have virtual machine, has helper class this:
public static class opcodes { private readonly static idictionary<byte, opcode> _opcodes; public static opcode mov { get; private set; } public static opcode add { get; private set; } public static opcode sub { get; private set; } static opcodes() { // opcode class contains info, e.g., number of arguments mov = new opcode(0x00, "mov", 2); add = new opcode(0x01, "add", 0); sub = new opcode(0x02, "sub", 0); _opcodes = new dictionary<byte, opcode> { { mov.code, mov }, { add.code, add }, { sub.code, sub }, }; } public static opcode get(byte code) { opcode result; if(_opcodes.trygetvalue(code, out result)) { return result; } return null; } }
as see, there lot of plumbing in there:
- a list of opcodes
- a static getter each opcode
- a way opcode it's binary value
the thing should have create #1, actual meat of application. wrapping in dictionary , creating static getters should done me.
i know "regular" visual studio has t4 templates, these pain run on build , seem tied visual studio , no longer applicable.
is there can do? think can create custom actions in project.json (e.g., dnx . regenopcodes
) don't know running here since need invoke roslyn, find list of opcodes (maybe make abstract opcodesbase
class), parse it, , generate concrete opcodes
class me.
alternatively, create opcodes.xml file , use normal text parsing create .cs it, i'm not sure how i'd configure anytime dnx . run
first runs generator , whatever normally.
you can add precompilation step, similar razor precompile views: https://github.com/aspnet/mvc/blob/64e726d2b26422c5452475627e4afaba307edec3/src/microsoft.aspnet.mvc/razorprecompilemodule.cs
here's example: https://github.com/aspnet/dnx/blob/e937e25ab3453ea86bda13c1297c41011dfda9de/ext/compiler/preprocess/internalization.cs
Comments
Post a Comment