这个问题涉及到这一块,但不是重复.Jb在那里发布了要添加自定义属性,以下代码段将起作用:
ModuleDefinition module = ...;
MethodDefinition targetMethod = ...;
MethodReference attributeConstructor = module.Import(
typeof(DebuggerHiddenAttribute).GetConstructor(Type.EmptyTypes));
targetMethod.CustomAttributes.Add(new CustomAttribute(attributeConstructor));
module.Write(...);
Run Code Online (Sandbox Code Playgroud)
我想使用类似的东西,但添加一个自定义属性,其构造函数在其(唯一)构造函数中采用两个字符串参数,并且我想为这些(显然)指定值.有人可以帮忙吗?
Jb *_*ain 12
首先,您必须获得对正确版本的构造函数的引用:
MethodReference attributeConstructor = module.Import(
typeof(MyAttribute).GetConstructor(new [] { typeof(string), typeof(string) }));
Run Code Online (Sandbox Code Playgroud)
然后,您只需使用字符串参数填充自定义属性:
CustomAttribute attribute = new CustomAttribute(attributeConstructor);
attribute.ConstructorArguments.Add(
new CustomAttributeArgument(
module.TypeSystem.String, "Foo"));
attribute.ConstructorArguments.Add(
new CustomAttributeArgument(
module.TypeSystem.String, "Bar"));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1436 次 |
最近记录: |