首页 > 解决方案 > Mono.Cecil - 向程序集添加新的属性类型

问题描述

我正在尝试使用 Mono.Cecil 向程序集添加新的属性类型。

我希望它看起来像:

internal class ConfusedByAttribute : Attribute
{
}

但是目前我在没有: Attribute(System.Attribute) 部分的情况下得到了相同的结果。

这是我的代码:

var assemblyDef = AssemblyDefinition.ReadAssembly(args[0]);
var tp = new TypeDefinition(
    "", 
    "ConfusedByAttribute",
    Mono.Cecil.TypeAttributes.NestedPrivate);
assemblyDef.MainModule.Types.Add(tp);
assemblyDef.Write(args[0] + "neW");

任何帮助表示赞赏,谢谢!

标签: c#monocode-injectionmono.cecil

解决方案


您需要添加 Attribute 作为基本类型:

tp.BaseType = assemblyDef.MainModule.ImportReference(typeof(Attribute));


推荐阅读