[Mono-dev] Setting Assembly's Attributes / Injecting new attributes

Jb Evain mono at evain.net
Sat Feb 17 00:30:52 EST 2007


Hi,

It's pretty easy to change such a value.

foreach (CustomAttribute ca in asm.CustomAttributes) {
	if (ca.Constructor.DeclaringType.Name != "AssemblyNameAttribute")
		continue;

	ca.ConstructorParameters [0] = "Hello World";
}

If you want to add a new one, just create a fully constructed 
CustomAttribute instance to the collection, and save the assembly.

Jb

Weston Weems wrote:
> Today I happened upon Mono.Cecil libraries and I've got to say, quite
> mindblowing. I've found something that lookes to be something Cecil
> should fairly easily be able to do, however I cant seem to deduce it
> from the documentation or lack thereof.
> 
> I've been able to open an assembly with the following
> 
> AssemblyDefinition sourceAssembly =
> AssemblyFactory.GetAssembly("MyAssembly.exe");
> 
> Which works and I can loop through the .CustomAttributes etc, but I
> cannot seem to figure out on my own how I can overwrite an existing
> attribute value, or how to inject a new custom attribute all together.
> 
> I was wondering if somone had a VERY simple example, that perhaps just
> changed "AssemblyNameAttribute" to hellow world or something.
> 
> I'd be greatly appreciative as my searching in news groups and such
> has kinda come to a dead end.
> 
> Thanks in advance.
> Weston
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list



More information about the Mono-devel-list mailing list