c# - 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");
任何帮助表示赞赏,谢谢!
解决方案
您需要添加 Attribute 作为基本类型:
tp.BaseType = assemblyDef.MainModule.ImportReference(typeof(Attribute));
推荐阅读
- python - 如何在 Python 中相互引用类
- sql - PostgreSQL 函数返回多种不同的类型
- android - 从 BLE 信标 (ANDROID) 获取单个消息
- c++ - 通过 DCOM 调用方法时的权限和/或安全问题
- json - 如何从 ansible 输出访问 JSON 值?
- gitlab - git@gitlab.com:权限被拒绝(公钥,键盘交互)
- reactjs - 在 Headless UI Popover.Button 右侧显示图标
- python - 如何在 html 模板中显示 plotly json 对象
- excel - VBA - 将 Excel 数据导入 MS Project
- java - 在 Spring Cloud GCP pubsub 中创建特定于消息通道的线程