[Mono-dev] Setting Assembly's Attributes / Injecting new attributes
Jb Evain
mono at evain.net
Sun Feb 18 05:28:40 EST 2007
Hey,
Please respond to the mailing list also.
Weston Weems wrote:
> A new CustomAttribute needs a MethodReference, which needs a
> TypeReference. Am I going the wrong route here? I am completely mind
> boggled. Should I create a customattribute like I would in straight
> c#?
>
> Looks like CustomAttribute in mono namespaces is not the same as
> CustomAttribute in .net framework.
Of course a Mono.Cecil.CustomAttribute is not the same thing as a
System.Reflection.CustomAttribute.
> I'd like to create a custom attribute thats literally just a string.
> Thats it thats all. I dont get the need for a method ref for a adding
> a string custom attribute to an assembly.
That's not possible. A CustomAttribute have to be of a type, and it have
to be constructed by a constructor. Obviously you need a method reference.
--
CustomAttribute ca = new CustomAttribute (
asm.MainModule.Import (
typeof (AssemblyDescriptionAttribute).GetConstructor (new Type []
{typeof (string)})));
ca.ConstructorParameters.Add ("Description");
asm.CustomAttributes.Add (ca);
--
Here is a sample code to create a brand new CustomAttribute of type
AssemblyDescriptionAttribute. To create the same with C# you would have
to write:
[AssemblyDescription("Description")]
Of course, it works also with your own attributes.
Jb
> ANyway thanks for any more information
>
> Wes
>
> On 2/16/07, Jb Evain <mono at evain.net> wrote:
>> 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