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

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

Post by Stripe » Sun, 17 Apr 2005 00:35:29


This is a multi-part message in MIME format.


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.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=koi8-r">
<META content="MSHTML 6.00.2900.2627" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff background="">
<DIV><FONT face=Arial size=2>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.</FONT></DIV>
<DIV><BR><FONT face=Arial size=2><STRONG>public static int Rule001() { 
return 1; } <BR></STRONG>==> <BR><STRONG>ILGenerator methodIL =
ruleMethod.GetILGenerator();<BR>methodIL.Emit(OpCodes.Ldc_I4, 
1);<BR>methodIL.Emit(OpCodes.Ret)</STRONG><BR></FONT></DIV>
<DIV><FONT face=Arial size=2>Thanks.</FONT></DIV></BODY></HTML>
 
 
 

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

Post by Stripe » Sun, 17 Apr 2005 02:31:36

This is a multi-part message in MIME format.


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.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=koi8-r">
<META content="MSHTML 6.00.2900.2627" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff background="">
<DIV><FONT face=Arial size=2>Oh,</FONT></DIV>
<DIV><FONT face=Arial size=2>I believe the simpliest solution is to create a
library and use ILDAsm on it.</FONT></DIV>
<DIV> </DIV>
<BLOCKQUOTE dir=ltr
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Striped" <<A


<DIV><FONT face=Arial size=2>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.</FONT></DIV>
<DIV><BR><FONT face=Arial size=2><STRONG>public static int Rule001() { 
return 1; } <BR></STRONG>==> <BR><STRONG>ILGenerator methodIL =
ruleMethod.GetILGenerator();<BR>methodIL.Emit(OpCodes.Ldc_I4, 
1);<BR>methodIL.Emit(OpCodes.Ret)</STRONG><BR></FONT></DIV>
<DIV><FONT face=Arial size=2>Thanks.</FONT></DIV></BLOCKQUOTE></BODY></HTML>

 
 
 

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

Post by Christian » Sun, 17 Apr 2005 20:01:08


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