473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

New to Reflection.Emit and need help

I am new to creating dynamic methods and I find that I have need to do
so. I have read through some IL and emit examples and have gotten some
simple programs to work. However, I cannot seem to use the opcode Call
correctly. This example gives me an "CLR detectd and invalid program"
exception at the line handler.invoke. I am sure the error is a simple
one. Could someone help me?

public partial class ILTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DynamicMethod handler = new DynamicMethod("", null, null,
typeof(ILTest).Module);
ILGenerator ilgen = handler.GetILGenerator();

MethodInfo miCall = typeof(ILTest).GetMethod("CallThis");
ilgen.Emit(OpCodes.Call, miCall);
ilgen.Emit(OpCodes.Ret);

handler.Invoke(this, null);
}

public void CallThis()
{
Response.Write("CALLED");
}
}

Thanks,
Chris Magoun

Apr 27 '07 #1
3 1559
My approach: code it as I would in C#, then load that dummy assembly
into Reflector (or ILDASM) and look at the IL. Job done.

Marc
Apr 27 '07 #2
On Apr 27, 11:48 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
My approach: code it as I would in C#, then load that dummy assembly
into Reflector (or ILDASM) and look at the IL. Job done.

Marc
I appreciate the advice: you got me one step further along. I have
modified the code to this:

public partial class ILTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DynamicMethod handler = new DynamicMethod("", null, null,
typeof(ILTest).Module);
ILGenerator ilgen = handler.GetILGenerator();

ilgen.Emit(OpCodes.Ldarg_0);
MethodInfo miCall = typeof(ILTest).GetMethod("CallThis", new
Type[0]);
ilgen.Emit(OpCodes.Call, miCall);
ilgen.Emit(OpCodes.Ret);

handler.Invoke(this, null);
}

public void CallThis()
{
this.Response.Write("Test");
}
}

Now the CallThis code fires, but there is one problem: this == null
and I get an "object not set" exception. I thought passing this to
Invoke meant that I was to calling the function on THAT instance. Any
ideas?

Thanks,
Chris

Apr 27 '07 #3
Just in case anyone in the future is looking for beginning IL
generation tips and finds this post in a search, here is the answer to
my problem:

The key is to realize that all methods created with DynamicMethod are
static and thus, it makes sense that the <thispointer == null. To
call the method on the desired instance, I have to pass it as a
parameter of the dynamic method like this:

public partial class ILTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//THIS LINE CHANGED
DynamicMethod handler = new DynamicMethod("", null, new Type[]
{typeof(ILTest)}, typeof(ILTest).Module);
ILGenerator ilgen = handler.GetILGenerator();

ilgen.Emit(OpCodes.Ldarg_0);

MethodInfo miCall = typeof(ILTest).GetMethod("CallThis");
ilgen.Emit(OpCodes.Call, miCall);
ilgen.Emit(OpCodes.Ret);

//THIS LINE CHANGED
handler.Invoke(null, new Object[] {this});
}

public void CallThis()
{
this.Response.Write("Test");
}
}

Notice the DynamicMethod will now take a single argument of type
ILTest, and that when I Invoke the method, I don't bind it to an
instance, but instead pass the desired <thispointer as a parameter.
This code works and invokes the dynamic method on the correct form
instance.

Thanks,
Chris

May 7 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nigel Sampson | last post by:
ey all, I'm in the process of creating a method of tracking whenever an objects properties are changed. Since its not possible to alter existing types I decided to used a proxy generated using...
0
by: samlee | last post by:
Hi All, I'm learning how to write C# using reflection, but don't know how to code using reflection this.Controls.Add(this.label1); Could anyone help, Thank in advance. ...
7
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the...
3
by: John Arthur | last post by:
Hi, I am reading about Reflection.Emit and I can't really imagine a real situation where I can use this. Can someone give me an example that can't be done without Reflection or an example of...
3
by: ayende | last post by:
How do I use Reflection.Emit to produce code similar to this? public void bar<T>() { } public void foo<J>() { bar<J>(); }
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
2
by: allfyre | last post by:
Ok, to make a long story short, I'm Emitting some MSIL from a c# application. I know that THIS is the MSIL I want to be emitting: //**************************************************************...
5
by: heddy | last post by:
I understand that reflection allows me to discover the metadata of a class at runtime (properties, methods etc). What I don't understand is where this is useful. For example: If I am the sole...
8
by: =?Utf-8?B?U2hhd24=?= | last post by:
Hi; i just started research reflection and i'm wondering if i have an empty class file can i use reflection to add member variables and attributes dynamically and then instantiate the class? What...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.