By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,470 Members | 1,081 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,470 IT Pros & Developers. It's quick & easy.

Reflection.Emit usage

P: n/a
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(newAssemN ame,
AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder =
asmBuilder.DefineDynamicModule("FromTwoTemplates") ;
TypeBuilder newType =
moduleBuilder.DefineType("TwoTemplatesMethods",
TypeAttributes.Class | TypeAttributes.Public);
newType.AddInterfaceImplementation(typeof(IOneAndT wo));

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.GetILAsByteArra y(),
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.GetILAsByteArra y(),
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[i] = parameterInfo[i].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\Progra m.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.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object
state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Any advices?

Thanks in advance
Mick

Oct 19 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.