473,846 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using System.Reflecti on.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 9983

"Mark Miller" <bl***@blank.co m> wrote in message
news:er******** ******@TK2MSFTN GP09.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::GetTypeFr omHandle. The literal IL is:

ldtoken Test
call class [mscorlib]System.Type
[mscorlib]System.Type::Ge tTypeFromHandle (valuetype
[mscorlib]System.RuntimeT ypeHandle)

There should be a overload for ILGenerator::Em it 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******** ******@TK2MSFTN GP09.phx.gbl...

"Mark Miller" <bl***@blank.co m> wrote in message
news:er******** ******@TK2MSFTN GP09.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::GetTypeFr omHandle. The literal IL is:

ldtoken Test
call class [mscorlib]System.Type
[mscorlib]System.Type::Ge tTypeFromHandle (valuetype
[mscorlib]System.RuntimeT ypeHandle)

There should be a overload for ILGenerator::Em it 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
ConstructorBuil der cb = tb.DefineConstr uctor(MethodAtt ributes.Public,
CallingConventi ons.Standard, new Type[1]{typeof(string) });
ILGenerator ilGen = cb.GetILGenerat or();

ilGen.Emit(OpCo des.Ldarg_0);
ilGen.Emit(OpCo des.Ldtoken, typeof(int));
ilGen.Emit(OpCo des.Call, typeof(Type).Ge tMethod("GetTyp eFromHandle", new
Type[1]{typeof(Runtime TypeHandle)}));
ilGen.Emit(OpCo des.Ldarg_1);

//pass arguments to base class constructor
ilGen.Emit(OpCo des.Call,
typeof(myParent ).GetConstructo r(BindingFlags. Instance |
BindingFlags.No nPublic, null, new Type[2]{typeof(Type), typeof(string)} ,
null));

ilGen.Emit(OpCo des.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::Ge tTypeFromHandle ([mscorlib]System.RuntimeT ypeHandle)
L_000b: ldarg.1
L_000c: call instance void
[HelloWorld-Emit]HelloWorld_Emit .myParent::.cto r([mscorlib]System.Type,
string)
L_0011: ret

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


"Mark Miller" <bl***@blank.co m> wrote in message
news:Oy******** ******@TK2MSFTN GP09.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.co m> wrote in message
news:OU******** ******@TK2MSFTN GP10.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
10024
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 language = Request.Form ("language"); Response.Write("Edition is type &quot;" + (typeof edition) + "&quot; and value &quot;" + edition + "&quot;<br>");
7
42578
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 = window.open('http://www.yahoo.com/'); alert(typeof x); And I found this page: http://www.mozilla.org/js/language/js20-2002-04/core/expressions.html
13
3098
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 across "null" string. I know that I will get an "undefined" when I try to retrieve something from the DOM which doesn't exist. I have used null myself to initialize or reset variables. But in which
4
5035
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 programmatically distinguish between an Array and an Option, for instance? Thanks
0
302
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 method. The method could look like this: public void myDynamicMethod(Type type){ //.....do something with the Type passed in....... }
5
8869
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 created methods are not "registered" and only available through the DynamicMethod class. So a method lookup finds the origin property. A little test: public class DerivedClass : BaseClass {
1
7123
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 someone who bothers to read all of it have some pointers. Note, I have posted the stack trace and the code exhibiting the problem further down so if you want to start by reading that, search for +++ Also note that I am unable to reproduce...
4
9868
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 this context, in fact some articles from pre-2.0 suggest using any collection class of your choice. So my questions focus more on the syntax of event properties provided by the "event" keyword in C#. (Disclaimer - I am a C++ programmer working...
1
2085
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
9879
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9725
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10705
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10330
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9477
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7877
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5906
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4111
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.