代码之家  ›  专栏  ›  技术社区  ›  Amy

使用Reflection.Emit匹配现有构造函数

  •  4
  • Amy  · 技术社区  · 14 年前

    首先,这里是C代码和反汇编的IL:

    public class Program<T>
    {
        private List<T> _items;
    
        public Program(T x, [Microsoft.Scripting.ParamDictionary] Microsoft.Scripting.IAttributesCollection col)
        {
            _items = new List<T>();
            _items.Add(x);
        }
    }
    

    以下是该构造函数的IL:

    .method public hidebysig specialname rtspecialname 
            instance void  .ctor(!T x,
                                 class [Microsoft.Scripting]Microsoft.Scripting.IAttributesCollection col) cil managed
    {
      .param [2]
      .custom instance void [Microsoft.Scripting]Microsoft.Scripting.ParamDictionaryAttribute::.ctor() = ( 01 00 00 00 ) 
      // Code size       34 (0x22)
      .maxstack  8
      IL_0000:  ldarg.0
      IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
      IL_0006:  nop
      IL_0007:  nop
      IL_0008:  ldarg.0
      IL_0009:  newobj     instance void class [mscorlib]System.Collections.Generic.List`1<!T>::.ctor()
      IL_000e:  stfld      class [mscorlib]System.Collections.Generic.List`1<!0> class Foo.Program`1<!T>::_items
      IL_0013:  ldarg.0
      IL_0014:  ldfld      class [mscorlib]System.Collections.Generic.List`1<!0> class Foo.Program`1<!T>::_items
      IL_0019:  ldarg.1
      IL_001a:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<!T>::Add(!0)
      IL_001f:  nop
      IL_0020:  nop
      IL_0021:  ret
    } // end of method Program`1::.ctor
    

    .method public hidebysig specialname rtspecialname 
            instance void  .ctor(!T A_1,
                                 class [Microsoft.Scripting]Microsoft.Scripting.IAttributesCollection A_2) cil managed
    {
      // Code size       34 (0x22)
      .maxstack  4
      IL_0000:  ldarg.0
      IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
      IL_0006:  ldarg.0
      IL_0007:  newobj     instance void class [mscorlib]System.Collections.Generic.List`1<!T>::.ctor()
      IL_000c:  stfld      class [mscorlib]System.Collections.Generic.List`1<!0> class MyType<!T>::_items
      IL_0011:  ldarg.0
      IL_0012:  ldfld      class [mscorlib]System.Collections.Generic.List`1<!0> class MyType<!T>::_items
      IL_0017:  ldarg.s    A_1
      IL_0019:  nop
      IL_001a:  nop
      IL_001b:  nop
      IL_001c:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<!T>::Add(!0)
      IL_0021:  ret
    } // end of method MyType::.ctor
    

    有一些区别我就是搞不懂。我真的很接近。。。

    1. 如何处理parameter属性(ParamDictionaryAttribute)?我找不到“自定义”操作码。

    2. .param[2]重要吗?我该怎么发射呢?

    2 回复  |  直到 14 年前
        1
  •  3
  •   Andrey    14 年前

    .custom 不是操作码,它是应用自定义属性的方法。这是宣言的一部分。它与 .param . .param[2] 告诉我们现在要讨论第二个参数。 应用指定的参数。看一看 MSIL spec

    设置参数调用的自定义属性 DefineParameter ParameterBuilder 呼叫 SetCustomAttribute()

        2
  •  1
  •   Lucero    14 年前

    -&燃气轮机;1./2.使用 DefineParameter() type[] ),然后你就可以 SetCustomAttribute()

    -&燃气轮机;3.我觉得这不重要。它基本上指定了该方法必须有多少堆栈才能运行。