Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / November 2007

Tip: Looking for answers? Try searching our database.

Reflection.Emit: Emitting open constructed generic method

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Florian Fordermaier - 26 Oct 2007 08:42 GMT
Hello Group,

assume the following sample model:

interface IGenericMethods
{
   void DoSomething<T>(T obj);
}

class GenericMethods
{
   public void DoSomething<T>(T obj)
   {
       Console.WriteLine(obj.GetType().ToString());
   }
}

I'm trying to dynamically emit a type(kind of a proxy) that implements
the interface IGenericMethods. In the dynamic DoSomething
implementation, the call shall just be delegated to an (existing)
instance(stored in a field) of class GenericMethods. The TypeBuilder
finishes without exceptions, but when calling the dynamic type's
DoSomething method I get an InvalidProgramException stating the CLR
has found an invalid program.
Comparing the IL of an handcrafted equivalent of what I'm trying to
generate with the actually generated IL shows no difference. My
question is: Is this not possible or are there some special things to
necessarily be aware of when emitting this kind of code.
Actually, this is just an example, the ultimate target is to generate
this kind of code for every kind of generic method, so I don't want to
generate code that just covers the example, but would be happy for
some advice on a general purpose procedure. I'm quite confused, since
everything >seems< to work fine, the IL is identical, but it's
throwing an exception when used.

Hope somebody can help me out here,
Thanks in advance
Florian
Ben Voigt [C++ MVP] - 01 Nov 2007 23:59 GMT
> Hello Group,
>
[quoted text clipped - 32 lines]
>
> Hope somebody can help me out here,

I remember having similar problems when I mixed up the TypeBuilder with the
Type returned from TypeBuilder.CreateType.  You'd think they both would
cause the same code to be generated, but it didn't actually work that way.
As nearly as I can tell, you must always pass in a System::Type for imported
types, and TypeBuilder for types defined in the same assembly.  Not sure if
this is your problem but it might be something to look for.

> Thanks in advance
> Florian

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.