Andrew Stakhov
Andrew Stakhov

Reputation: 1135

Adding assembly attribute with Mono.Cecil throws

So I'm trying to add ReferenceAssemblyAttribute to assembly with Mono.Cecil. Here's my code:

var assembly = AssemblyDefinition.ReadAssembly(inputDll);
var referenceAssemblyAttribute = assembly.MainModule.ImportReference(typeof(ReferenceAssemblyAttribute));
var referenceAssemblyAttributePrimaryConstructor = referenceAssemblyAttribute.Resolve().GetConstructors().First(x => !x.HasParameters);
assembly.MainModule.ImportReference(referenceAssemblyAttributePrimaryConstructor);
assembly.CustomAttributes.Add(new CustomAttribute(referenceAssemblyAttributePrimaryConstructor));
assembly.Write(outputDll);

this throws with Unhandled exception. System.ArgumentException: Member 'System.Void System.Runtime.CompilerServices.ReferenceAssemblyAttribute::.ctor()' is declared in another module and needs to be imported

I can see that the assembly.MainModule.AssemblyReferences gets populated with an additional reference to System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e which is where the attribute is defined.

What am I doing wrong?

Upvotes: 0

Views: 28

Answers (0)

Related Questions