Using a DSL to generate C# Code

前端 未结 3 602
面向向阳花
面向向阳花 2021-02-09 19:00

Currently the project I\'m working with does not have completely fixed models (due to an external influence) and hence I\'d like some flexibility in writing them. Currently they

相关标签:
3条回答
  • 2021-02-09 19:17

    I think you are on the right track.

    What I usually do in a situation like this is design a simple language that captures my needs and write a LL1 (Recursive Descent) parser for it.

    If the language has to have non-trivial C# syntax in it, I can either quote that, or just wrap it in brackets that I can recognize, and just pass it through to the output code.

    I can either have it generate a parse tree structure, and generate say 3 different kinds of code from that, or I can just have it generate code on the fly, either using a mode variable with 3 values, or just simultaneously write code to 3 different output files.

    There's more than one way to do it. If you are afraid of writing parsers (as some programmers are), there is lots of help elsewhere on SO.

    0 讨论(0)
  • 2021-02-09 19:23

    I have seen a system that used partial classes and partial methods to allow for regeneration of code without affecting custom code. The "rules engine" if you will was completely generated from a Visio state diagram. This is basically poor mans workflow but very easy to modify. The Viso diagram was exported to XML which was read in using powershell and T4 to generate the classes.

    The above example is of an external DSL. I.E. external to the programming language that the application runs in. You could on the other hand create an internal DSL which is implemented and used in a programming language.

    This and the previous article on DSLSs from Code-Magazine are quite good.

    In the above link Neal Ford shows you how to create an internal DSL in C# using a fluent interface.

    One thing he hasn't mentioned yet is that you can put this attribute [EditorBrowsable(EditorBrowsableState.Never)] on your methods so that they don't appear to intellisense. This means that you can hide the non-DSL (if you will) methods on the class from the user of the DSL making the fluent API much more discoverable.

    You can see a fluent interface being written live in this video series by Daniel Cazzulino on writing an IoC container with TDD

    On the subject of external DSLs you also have the option of Oslo (CTP at the moment) which is quite powerful in it's ability to let you create external DSLs that can be executed directly rather than for the use of code generation which come to think of it isn't really much of a DSL at all.

    0 讨论(0)
  • 2021-02-09 19:30

    This can be easily done with ANTLR. If the output is similar enough you can simply use the text templating mechanism—otherwise it can generate an abstract syntax tree for you to traverse.

    0 讨论(0)
提交回复
热议问题