views:

235

answers:

5
+1  Q: 

C# code generator

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  A: 

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.

Gabe
@Gabe: why isn't there a compiler within the framework? Take a look at: http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx
Oliver
I am writing tool which help me automatize typical every day tasks.
Neir0
@Oliver I think that compiler is not a part of framework. it's external program (csc.exe) instead c# compiler in mono for example.
Neir0
As Neir0 said, all it does is call csc and give you an assembly back. If that's what you're looking for, that's fine. It doesn't seem to be what question was asking. I assumed the question was asking for something like DynamicQuery only accepting full C# syntax.
Gabe
+3  A: 

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

280Z28
CodeDOM is too weighty and difficult to study. I am looking something more easy and light
Neir0
@Neir0: It's about as easy as it gets, I'm baffled as to what you're looking for if you call this one too difficult.
280Z28
+1  A: 

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.

Anders Abel
+4  A: 

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.

Romain Verdier
Thanks. But this tool doesn't help to generate code
Neir0
A: 

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.

tobsen