C# code generator [closed]

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 01:40:29

问题


Can someone recommend a simple c# code generator. I just looking something with methods like:

GenClass = CreateNewClass(AccessModifier,Name......)

GenClass.Add(new Method(AccessModifier,RetType,Name....){code=@"....."}

GenClass.Add(new Property(AccessModifier,Type, Name....)

........... etc

and after creating all classes\methods and other members we call Code Generation function(where we can specific some parametrs)

Is there such opensource code generator?


回答1:


Check out Using CodeDOM to generate CSharp (C#) and VB code




回答2:


T4 or Text Template Transformation Toolkit might be worth looking into.

Another option is to create your own simple generator, which contains functionality more suited for your situation than the CodeDOM. In a recent code generation project that's what I did, however I have encapsulated the code generation to make it possible to later transition to CodeDOM.




回答3:


You may want to have a look csscript that relies on CodeDOM.

It allows you to write things like:

var PrintSum = CSScript.LoadMethod(
        @"public static void PrintSum(int a, int b)
          {
              Console.WriteLine((a+b));
          }")
          .GetStaticMethod();
PrintSum(1, 2);

Be sure to read the doc, it's pretty detailed and you'll find you can do a lot more than what I just copied before.




回答4:


If you want to be able to generate a class given some arbitray string containing C# code, you need a C# compiler. At the moment the .Net framework does not ship with a compiler that you can pass snippets of C# to and get compiled code back. If you have more specific needs, you should specify exactly what you're looking to do.




回答5:


Since you explicitly searching for an opensource code generator I suggest MyGeneration. Another, template based approach (which is not what you are looking for since want "GenClass.Add...." syntax rather than templates) would be Codesmith Tools it's really powerful but closed source.




回答6:


take a look at my open source generator http://code.google.com/p/magicapps/



来源:https://stackoverflow.com/questions/2536031/c-sharp-code-generator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!