Injecting GeneratedCodeAttribute with Mono.Cecil

╄→гoц情女王★ 提交于 2019-12-08 02:51:40

问题


I'm manupulating my .net 2.0 assemblies with Mono.Cecil. After manipulation I want to mark assembly as processed by injecting a module attribute

var stringType = _module.Import(typeof(string));
var baseCtor = _module.Import(typeof(GeneratedCodeAttribute).GetConstructor(new[] { typeof(string), typeof(string) }));
var result = new CustomAttribute(baseCtor);
result.ConstructorArguments.Add(new CustomAttributeArgument(stringType, "ProcessedBySomething"));
result.ConstructorArguments.Add(new CustomAttributeArgument(stringType, "1.0"));

After saving the assembly it become dependent on .net 4.0, since manipulating app is written in .net 4.0. GeneratedCodeAttribute exists in .net 2.0, so what am I doing wrong?


回答1:


You're guessing right. Since the manipulating application is running on .net 4.0, typeof being a runtime feature, it will return a type for the current runtime version.

To fix it, the simple thing to do is to create references for the mscorlib version referenced by the module you're modifying, using Cecil to open the assembly. Your code would become:

var stringType = _module.TypeSystem.String;
var corlib = (AssemblyNameReference) _module.TypeSystem.Corlib;
var system = _module.AssemblyResolver.Resolve (new AssemblyNameReference ("System", corlib.Version) {
    PublicKeyToken = corlib.PublicKeyToken,
});
var generatedCodeAttribute = system.MainModule.GetType ("System.CodeDom.Compiler.GeneratedCodeAttribute");
var generatedCodeCtor = generatedCodeAttribute.Methods.First (m => m.IsConstructor && m.Parameters.Count == 2);

var result = new CustomAttribute (_module.Import (generatedCodeCtor));
result.ConstructorArguments.Add(new CustomAttributeArgument(stringType, "ProcessedBySomething"));
result.ConstructorArguments.Add(new CustomAttributeArgument(stringType, "1.0"));


来源:https://stackoverflow.com/questions/4712265/injecting-generatedcodeattribute-with-mono-cecil

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!