3

I want to write a code generator and save these codes with mvp pattern, can I use Reflection.Emit as a solution or CodeDom is better?

EDIT--------------

I have to do 2 work, firstly I want to compile this code at runtime and run it and secondly generate the source code as an option.

masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
  • 2
    I didn't downvote. This is a question that a lot of people have. Maybe you should expand more about what kind of code you're trying to generate (source, expected output). Also, look at the T4 template engine.http://msdn.microsoft.com/en-us/library/bb126445.aspx – Joseph Yaduvanshi Jun 28 '11 at 03:34
  • What is your reason for downvoting? – masoud ramezani Jun 28 '11 at 03:35
  • I have to done 2 work, firstly I want to compile this code at runtime and run it and secondly generate the source code as an option. – masoud ramezani Jun 28 '11 at 03:40
  • I recently learnt that dont tag several groups especially c#. Then they will down vote your question. – madufit1 Oct 25 '17 at 09:57

2 Answers2

3

Although you can achieve some aspects of your question with both Namespaces (System.CodeDom and System.Reflection.Emit) you can't mix the two different concepts and use the option(s) that better fits whatever you want to do.

If you just want to generate and compile C#\VB code maybe you want do use System.CodeDom:

This namespace can be used to model the structure of a source code document that can be output as source code in a supported language using the functionality provided by the System.CodeDom.Compiler namespace.

Check this small example on how to define a method through CodeDom:

// Defines a method that returns a string passed to it.
CodeMemberMethod method1 = new CodeMemberMethod();            
method1.Name = "ReturnString";
method1.ReturnType = new CodeTypeReference("System.String");
method1.Parameters.Add( new CodeParameterDeclarationExpression("System.String", "text") );
method1.Statements.Add( new CodeMethodReturnStatement( new CodeArgumentReferenceExpression("text") ) );            

// A C# code generator produces the following source code for the preceeding example code:

//    private string ReturnString(string text) 
//    {
//        return text;
//    }

As for the System.Reflection.Emit its used to generate and manipulate MSIL (Microsoft Intermediate Language) which is lowest-level human-readable programming language defined by the Common Language Infrastructure specification and used by the .NET Framework. Note that MSIL is now known as CIL (Common Intermediate Language):

The System.Reflection.Emit namespace contains classes that allow a compiler or tool to emit metadata and Microsoft intermediate language (MSIL) and optionally generate a PE file on disk. The primary clients of these classes are script engines and compilers.

The use of this approach leads you to learn CIL bytecode instructions which is a rather difficult and complex task even if you're familiar with assembly code and might not be ideal if what you want is rather simple and fast to implement. Check wikipedia article on CIL and CIL instructions list for a first impression.

Joao
  • 7,366
  • 4
  • 32
  • 48
2

If you're going to want the option to see the C# source code, I say go with CodeDom. Reflection.Emit renders IL, but getting that to usable C# code is not going to be easy.

Tremmors
  • 2,906
  • 17
  • 13