Hi Fabian,

The IL error you get is due to usage of !!0 outside of generic instantiation 
context. You simply don't have type parameter #0 in this context.

Try the following:
    ldtoken    method void PreloadMethodInfo.Program::Generic<class .this>()

HTH,
Serge

-----Original Message-----
From: Discussion of advanced .NET topics. [mailto:[EMAIL PROTECTED] On Behalf 
Of Fabian Schmied
Sent: Monday, April 23, 2007 2:35 AM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] How to do ldtoken on generic method specs in IL

This is a very specific question, but maybe there is an IL expert
reading this, so: Does anyone know whether there is a way to load the
token of an open generic method (i.e. with unbound generic arguments)
from IL?

Consider, for example, the following IL program:

.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
  .ver 2:0:0:0
}
.assembly PreloadMethodInfo
{
  .hash algorithm 0x00008004
  .ver 1:0:0:0
}
.module PreloadMethodInfo.exe

.class private auto ansi beforefieldinit PreloadMethodInfo.Program
       extends [mscorlib]System.Object
{
  .method public hidebysig static void  Main(string[] args) cil managed
  {
    .entrypoint
    .locals init ([0] class [mscorlib]System.Reflection.MethodInfo 'method')

    ldtoken    method void class PreloadMethodInfo.Program::Generic<!!0>()
    ldtoken    class PreloadMethodInfo.Program
    call       class [mscorlib]System.Reflection.MethodBase
[mscorlib]System.Reflection.MethodBase::GetMethodFromHandle(valuetype
[mscorlib]System.RuntimeMethodHandle,

                                                         valuetype
[mscorlib]System.RuntimeTypeHandle)
    castclass  [mscorlib]System.Reflection.MethodInfo

    stloc.0
    ldloc.0
    call       void [mscorlib]System.Console::WriteLine(object)
    ret
  }

  .method public hidebysig static void  Generic<T>() cil managed
  {
    // Code size       2 (0x2)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ret
  } // end of method Program::Generic
} // end of class PreloadMethodInfo.Program

This assembles, but cannot be verified; the verification error is:

[IL]: Error: [... : PreloadMethodInfo.Program::Main][offset
0x00000000][token  0x2B000001]System.BadImageFormatException: An
attempt was made to load a program with an incorrect format.
(Exception from HRESULT: 0x8007000B)  [HRESULT 0x8007000B] - An
attempt was made to load a program with an incorrect format.

I'm probably using using an invalid syntax for loading the token, but
can't figure out how to do this correctly. Or maybe this is simply
forbidden by the CLR. Can someone shed light on this? If it is
forbidden, is there something in the CLI specification to back this?
(Because I can't seem to find it.)

Fabian

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to