C# 如何定义';价值';属性设置器参数
使用以下C#代码: 属性设置程序签名编译为:C# 如何定义';价值';属性设置器参数,c#,cil,reflection.emit,C#,Cil,Reflection.emit,使用以下C#代码: 属性设置程序签名编译为: .method public hidebysig specialname newslot abstract virtual instance void set_X ( int32 'value' ) cil managed { } 使用ILSpy或ildasm检查时 如果我尝试使用System.Reflection.EmitAPI生成相同的方法签名,则生成的输入参数名称为空: .method public hid
.method public hidebysig specialname newslot abstract virtual
instance void set_X (
int32 'value'
) cil managed
{
}
使用ILSpy或ildasm检查时
如果我尝试使用System.Reflection.Emit
API生成相同的方法签名,则生成的输入参数名称为空:
.method public hidebysig specialname newslot abstract virtual
instance void set_X (
int32 ''
) cil managed
{
}
(签名由ilspy
生成)
。。。或看似生成的引用名称(a_1
,在本例中):
(签名由ildasm
生成)
我怎样才能像C#编译示例中那样给输入参数命名为“value”
下面是我用来生成setter的代码:
PropertyBuilder property = typeDef.DefineProperty("X", PropertyAttributes.HasDefault, CallingConventions.HasThis, typeof(int), null);
MethodAttributes ma = MethodAttributes.Public
| MethodAttributes.HideBySig
| MethodAttributes.NewSlot
| MethodAttributes.SpecialName
| MethodAttributes.Abstract
| MethodAttributes.Virtual;
MethodBuilder setMethod = typeDef.DefineMethod("set_X", ma, CallingConventions.HasThis, null, new[] { typeof(int) });
property.SetSetMethod(setMethod);
即使我显式尝试定义参数名称,结果仍然相同:
MethodBuilder setMethod = typeDef.DefineMethod("set_X", ma, CallingConventions.HasThis, null, new[] { typeof(int) });
ParameterBuilder pb = setMethod.DefineParameter(0, ParameterAttributes.None, "value");
property.SetSetMethod(setMethod);
我认为您必须使用index
1
作为第一个参数。从msdn条目中:
备注
[……]
参数编号以1开头,因此第一个参数的位置为1。如果位置为零,此方法将影响返回值
就这样!非常感谢。
PropertyBuilder property = typeDef.DefineProperty("X", PropertyAttributes.HasDefault, CallingConventions.HasThis, typeof(int), null);
MethodAttributes ma = MethodAttributes.Public
| MethodAttributes.HideBySig
| MethodAttributes.NewSlot
| MethodAttributes.SpecialName
| MethodAttributes.Abstract
| MethodAttributes.Virtual;
MethodBuilder setMethod = typeDef.DefineMethod("set_X", ma, CallingConventions.HasThis, null, new[] { typeof(int) });
property.SetSetMethod(setMethod);
MethodBuilder setMethod = typeDef.DefineMethod("set_X", ma, CallingConventions.HasThis, null, new[] { typeof(int) });
ParameterBuilder pb = setMethod.DefineParameter(0, ParameterAttributes.None, "value");
property.SetSetMethod(setMethod);