How to convert source code to opcodes used in Reflection.Emit?

  • Thread starter Thread starter Striped
  • Start date Start date
S

Striped

Is there any tool to convert source code, e.g. of a C# method, to opcodes being used in System.Reflection.Emit? It's a real pain to do that manually, especially for big pieces of code.

public static int Rule001() { return 1; }
==>
ILGenerator methodIL = ruleMethod.GetILGenerator();
methodIL.Emit(OpCodes.Ldc_I4, 1);
methodIL.Emit(OpCodes.Ret)

Thanks.
 
Oh,
I believe the simpliest solution is to create a library and use ILDAsm on it.

Is there any tool to convert source code, e.g. of a C# method, to opcodes being used in System.Reflection.Emit? It's a real pain to do that manually, especially for big pieces of code.

public static int Rule001() { return 1; }
==>
ILGenerator methodIL = ruleMethod.GetILGenerator();
methodIL.Emit(OpCodes.Ldc_I4, 1);
methodIL.Emit(OpCodes.Ret)

Thanks.
 
Striped said:
Is there any tool to convert source code, e.g. of a C# method, to
opcodes being used in System.Reflection.Emit? It's a real pain to do
that manually, especially for big pieces of code.

*public static int Rule001() { return 1; }
*==>
*ILGenerator methodIL = ruleMethod.GetILGenerator();
methodIL.Emit(OpCodes.Ldc_I4, 1);
methodIL.Emit(OpCodes.Ret)*
Thanks.
Have you looked at the Microsoft.CSharp.CSharpCodeProvider class?
It derives from the System.CodeDom.Compiler namespace and that makes it
possible to dynamically compile .NET code on the fly resulting in an
assembly that could be executed.

Is this what you were looking for?

Christian
 
Back
Top