,带有以下C#代码:
public interface IFoo
{
int Bar
{
get;
set;
}
}
属性设置器签名汇编为:
.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
):
.method public hidebysig newslot specialname abstract virtual
instance void set_X(
int32 A_1
) cil managed
{
}
(ildasm
生成的签名)
如何将输入参数称为" value",例如c#编译示例?
这是我用来生成设置器的代码:
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);
我认为您必须使用索引1
进行第一个参数。从MSEDBUILDER的MSDN条目。DefineParameter方法:
备注
[...]
参数编号从1开始,因此第一个参数为 so 位置为1。如果位置为零,则此方法会影响返回值。