473,407 Members | 2,312 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,407 software developers and data experts.

Using System.Reflection.Emit to create a call to "typeof()"

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 method.

The method could look like this:

public void myDynamicMethod(Type type){
//.....do something with the Type passed in.......
}

the call to the method would look like this:

myDynamicMethod( typeof( myCustomType ) );

So essentially I am looking to use Emit to output the "typeof(
myCustomType )" part. Any suggestions would be appreciated.

Mark
Nov 16 '05 #1
7 9913

"Mark Miller" <bl***@blank.com> wrote in message
news:er**************@TK2MSFTNGP09.phx.gbl...
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 method.

The method could look like this:

public void myDynamicMethod(Type type){
//.....do something with the Type passed in.......
}

the call to the method would look like this:

myDynamicMethod( typeof( myCustomType ) );

So essentially I am looking to use Emit to output the "typeof(
myCustomType )" part. Any suggestions would be appreciated.

If I understand you, you would emit a Ldtoken instruction and then call
Type::GetTypeFromHandle. The literal IL is:

ldtoken Test
call class [mscorlib]System.Type
[mscorlib]System.Type::GetTypeFromHandle(valuetype
[mscorlib]System.RuntimeTypeHandle)

There should be a overload for ILGenerator::Emit that takes a type as the
second parameter, which is what you would use to emit ldtoken. Mark

Nov 16 '05 #2
Thanks, for the quick response. I'll try that right away.

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eY**************@TK2MSFTNGP09.phx.gbl...

"Mark Miller" <bl***@blank.com> wrote in message
news:er**************@TK2MSFTNGP09.phx.gbl...
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 method.

The method could look like this:

public void myDynamicMethod(Type type){
//.....do something with the Type passed in.......
}

the call to the method would look like this:

myDynamicMethod( typeof( myCustomType ) );

So essentially I am looking to use Emit to output the "typeof(
myCustomType )" part. Any suggestions would be appreciated.


If I understand you, you would emit a Ldtoken instruction and then call
Type::GetTypeFromHandle. The literal IL is:

ldtoken Test
call class [mscorlib]System.Type
[mscorlib]System.Type::GetTypeFromHandle(valuetype
[mscorlib]System.RuntimeTypeHandle)

There should be a overload for ILGenerator::Emit that takes a type as the
second parameter, which is what you would use to emit ldtoken.
Mark


Nov 16 '05 #3
I'm having trouble translating your il into Emit commands. I didn't say
before but I am new to the Reflection.Emit namespace and so if you could
interpret the IL for me I would appreciate it.

thanks,
Mark
Nov 16 '05 #4
Nevermind, it looks like a good nights sleep took care of everything. I had
been using Lutz Roeder's .Net Reflector and handn't been looking at IL to
view the results of my Emit code. I hand been looking at C# output. I
switched to IL mode and from what you said, I was able to compare the IL I
was trying to get against other code I had written, and that did it.

Since I wasn't able to find any other examples of this anywhere, here's the
solution:

I had actually been trying to call the protected base constructor from an
inherited class. The base constructor had a parameter that I wanted to hide
but I wanted to expose the other parameters in a public constructor. This is
what I wanted to see:

public myClass(string SomeParam) : base ( typeof( someClass ), SomeParam ){}

Here's the Emit code to achieve the result:

------------------------------ BEGIN
CODE -------------------------------------------
//build a constructor which passes an argument of type Type to base
constructor by calling typeof
ConstructorBuilder cb = tb.DefineConstructor(MethodAttributes.Public,
CallingConventions.Standard, new Type[1]{typeof(string)});
ILGenerator ilGen = cb.GetILGenerator();

ilGen.Emit(OpCodes.Ldarg_0);
ilGen.Emit(OpCodes.Ldtoken, typeof(int));
ilGen.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", new
Type[1]{typeof(RuntimeTypeHandle)}));
ilGen.Emit(OpCodes.Ldarg_1);

//pass arguments to base class constructor
ilGen.Emit(OpCodes.Call,
typeof(myParent).GetConstructor(BindingFlags.Insta nce |
BindingFlags.NonPublic, null, new Type[2]{typeof(Type), typeof(string)},
null));

ilGen.Emit(OpCodes.Ret);
------------------------------ END
CODE -----------------------------------------------

Here's the IL (using Lutz Roeder's Reflector):

----------------------- BEGIN IL ----------------------------------------
..method public specialname rtspecialname instance void .ctor(string) cil
managed
{
// Code Size: 18 byte(s)
..maxstack 3
L_0000: ldarg.0
L_0001: ldtoken int32
L_0006: call [mscorlib]System.Type
[mscorlib]System.Type::GetTypeFromHandle([mscorlib]System.RuntimeTypeHandle)
L_000b: ldarg.1
L_000c: call instance void
[HelloWorld-Emit]HelloWorld_Emit.myParent::.ctor([mscorlib]System.Type,
string)
L_0011: ret

}
--------------------END IL -----------------------------------------------


"Mark Miller" <bl***@blank.com> wrote in message
news:Oy**************@TK2MSFTNGP09.phx.gbl...
I'm having trouble translating your il into Emit commands. I didn't say
before but I am new to the Reflection.Emit namespace and so if you could
interpret the IL for me I would appreciate it.

thanks,
Mark

Nov 16 '05 #5

"Mark Miller" <bl***@blank.com> wrote in message
news:OU**************@TK2MSFTNGP10.phx.gbl...
Nevermind, it looks like a good nights sleep took care of everything. I
had
been using Lutz Roeder's .Net Reflector and handn't been looking at IL to
view the results of my Emit code. I hand been looking at C# output. I
switched to IL mode and from what you said, I was able to compare the IL I
was trying to get against other code I had written, and that did it.


Good, looks correct to me. Sorry I didn't provide ILGenerator examples, but
itappears it didn't matter.
Nov 16 '05 #6
Mark,

The best way to solve this kind of problems is to code a sample class compile it and than use ILDASM or Reflector.NET to see the generated IL. (at least this is how i do it)

Pedro

************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 16 '05 #7
Mark,

The best way to solve this kind of problems is to code a sample class compile it and than use ILDASM or Reflector.NET to see the generated IL. (at least this is how i do it)

Pedro

************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 16 '05 #8

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

Similar topics

8
by: Robert Mark Bram | last post by:
Hi All! I have the following code in an asp page whose language tag is: <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> // Find request variables. var edition = Request.Form ("edition"); var...
7
by: Bennett Haselton | last post by:
Is there any way to find a string representing an object's class, which will work in Internet Explorer 6? "typeof" doesn't work -- it returns "object" for all objects: x =...
13
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
4
by: Tim Johnson | last post by:
Hello All: I would like to expand upon the typeof operator. Using 'typeof' I can distinguish between string and array for instance, because typeof returns "object" for array. How can I...
0
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...
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
4
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
1
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi This is from within an EXE that actaully contains the class - No external assembly I would do Class1 myClass = new Class1(); Now I want
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.