C# code generator [closed]

2020-03-01 08:00发布

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?

6条回答
时光不老,我们不散
2楼-- · 2020-03-01 08:28

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.

查看更多
家丑人穷心不美
3楼-- · 2020-03-01 08:34

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.

查看更多
霸刀☆藐视天下
4楼-- · 2020-03-01 08:35

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.

查看更多
疯言疯语
5楼-- · 2020-03-01 08:36

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.

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2020-03-01 08:45

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

查看更多
登录 后发表回答