Reflection.Emit usage

M

Mick

Hello all!
Can anybody provide a good example for the Reflectio.Emitl namespace
usage? The problem I'm trying to solve is copying the method body from
existing class to newly created one.
Below is the code targeting .Net 2.0 , that I'm trying to use without
success:

class TemplateOne
{
public void PrintTemplateOne()
{
Console.WriteLine("I'm the method of template #1");
}
}

class TemplateTwo
{
public void PrintTemplateTwo()
{
Console.WriteLine("I'm the method of template #2");
}
}

public interface IOneAndTwo
{
void PrintTemplateOne();
void PrintTemplateTwo();
}

class Program
{
static void Main(string[] args)
{
AssemblyName newAssemName = new
AssemblyName("FromTwoTemplates");
AssemblyBuilder asmBuilder =
Thread.GetDomain().DefineDynamicAssembly(newAssemName,
AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder =
asmBuilder.DefineDynamicModule("FromTwoTemplates");
TypeBuilder newType =
moduleBuilder.DefineType("TwoTemplatesMethods",
TypeAttributes.Class | TypeAttributes.Public);
newType.AddInterfaceImplementation(typeof(IOneAndTwo));

MethodBuilder method1Builder =
newType.DefineMethod(typeof(IOneAndTwo).GetMethods()[0].Name,
MethodAttributes.Public | MethodAttributes.Virtual,
typeof(IOneAndTwo).GetMethods()[0].ReturnType,

GetParameterTypes(typeof(IOneAndTwo).GetMethods()[0].GetParameters()));

MethodBody mb =
typeof(TemplateOne).GetMethods()[0].GetMethodBody();
method1Builder.CreateMethodBody(mb.GetILAsByteArray(),
mb.GetILAsByteArray().Length);

MethodBuilder method2Builder =
newType.DefineMethod(typeof(IOneAndTwo).GetMethods()[1].Name,
MethodAttributes.Public | MethodAttributes.Virtual,
typeof(IOneAndTwo).GetMethods()[1].ReturnType,

GetParameterTypes(typeof(IOneAndTwo).GetMethods()[1].GetParameters()));

mb = typeof(TemplateTwo).GetMethods()[0].GetMethodBody();
method2Builder.CreateMethodBody(mb.GetILAsByteArray(),
mb.GetILAsByteArray().Length);

newType.DefineMethodOverride(method1Builder,
typeof(IOneAndTwo).GetMethods()[0]);
newType.DefineMethodOverride(method2Builder,
typeof(IOneAndTwo).GetMethods()[1]);

Type created = newType.CreateType();

IOneAndTwo oneTwo1 = asmBuilder.CreateInstance(newType.Name)
as IOneAndTwo;

Console.WriteLine("Copied methods:");
oneTwo1.PrintTemplateOne();
oneTwo1.PrintTemplateTwo();
Console.ReadKey();
}

private static Type[] GetParameterTypes(ParameterInfo[]
parameterInfo)
{
Type[] paramTypes = new Type[parameterInfo.Length];
for (int i = 0; i < parameterInfo.Length; i++)
{
paramTypes = parameterInfo.ParameterType;
}
return paramTypes;
}
}

When it comes to the first method invocation (PrintTemplateOne) I get
the following exception:

System.BadImageFormatException was unhandled
Message="Bad method token."
Source="FromTwoTemplates"
StackTrace:
at TwoTemplatesMethods.PrintTemplateOne()
at ReflectionTest.Program.Main(String[] args) in
D:\Documents\Visual Studio
2005\Projects\ReflectionTest\ReflectionTest\Program.cs:line 50
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Any advices?

Thanks in advance
Mick
 
M

Mattias Sjögren

Can anybody provide a good example for the Reflectio.Emitl namespace
usage? The problem I'm trying to solve is copying the method body from
existing class to newly created one.

The raw IL byte stream you get with MethodBody.GetILAsByteArray()
contain token values that are only valid within the same executable
module. You can't take those raw tokens and write them to another
method body and expect it to work.


Mattias
 
M

Mick

First of all, thanks for some clarification
The raw IL byte stream you get with MethodBody.GetILAsByteArray()
contain token values that are only valid within the same executable
module. You can't take those raw tokens and write them to another
method body and expect it to work.

So is there some way to modify those tokens to make them relevant in
the new module context?
Or is there any way I can identify them in the IL stream?

Mick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top