如何在 ConstructorBuilder 中定义局部变量?
我想为运行时构建的对象创建一个构造函数,该构造函数调用一个方法,该方法采用传递给构造函数的所有参数的对象数组。看看如何构建这样的方法,看来我必须做类似的事情::
method pulbic hidebysig specialname rtspecialname instance void .ctor(SomeObject arg) cil managed
{
ldarg.0
call void MyNameSpace.BaseClass::.ctor();
ldc.i4.1
newarr System.Object
stloc.0
ldloc.0
ldc.i4.0
ldarg.1
stelem.ref
ldloc.0
call void SomeClass.Method(object[])
ret
}
相当简单,但是如何在 ctor 构建器上定义 object[]
类型的局部变量?似乎没有办法定义变量,或者我只是发出一个 stloc.0/ldloc.0
而不关心首先定义它?
I'd like to make a constructor for my object built at runtime that calls a method that takes an object array of all the parameters passed to the ctor. Looking at how to build such a method it appears I have to do something like::
method pulbic hidebysig specialname rtspecialname instance void .ctor(SomeObject arg) cil managed
{
ldarg.0
call void MyNameSpace.BaseClass::.ctor();
ldc.i4.1
newarr System.Object
stloc.0
ldloc.0
ldc.i4.0
ldarg.1
stelem.ref
ldloc.0
call void SomeClass.Method(object[])
ret
}
Fairly straightforward, but how do I define a local variable of type object[]
on the ctor builder? There doesn't appear to be a way to define variables, or do I just emit a stloc.0/ldloc.0
and not care about defining it first?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能调用
GetILGenerator()
,然后使用它来使用ILGenerator.DeclareLocal
声明局部变量吗?诚然,我还没有尝试过使用其中任何一个,但它听起来就像是要走的路......
Can't you call
GetILGenerator()
and then use that to declare a local variable usingILGenerator.DeclareLocal
?Admittedly I haven't tried using any of this, but it sounds like it's the way to go...