C# code generator [closed]
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 开发者_JS百科classes\methods and other members we call Code Generation function(where we can specific some parametrs)
Is there such opensource code generator?
Check out Using CodeDOM to generate CSharp (C#) and VB code.
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.
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.
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.
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.
take a look at my open source generator http://code.google.com/p/magicapps/
精彩评论