Thanks for the quick response as always.
> If you have a CustomAttribute.ConstructorParameters collection, it
> means that you're using an old version of Mono.Cecil. I strongly
> suggest you use a recent one fromhttp://github.com/jbevain/cecil.
>
> If you have a CustomAttribute.ConstructorArguments collections, you're good.
I am using the Nuget published on 12/2/2012 v. 0.9.5.2.
I have both CustomAttribute.ConstructorArguments AND
CustomAttribute.Constructor.Parameters, a quick check of the source on
GitHub reveals that is still the case on the latest branch. Perhaps
you misread my comment and missed the dot between the CustomAttribute
and Parameters?
> And if you want to set properties, you can not use constructor
> arguments. Remember that a custom attributes is mapped to a
> constructor invocation, plus setting some fields and properties. So in
> your example, AttachDataAttribute has default constructor with no
> parameter, and thus, no argument is passed to instantiate it.
Yes, I get that. I don't want to use constructor arguments
deliberately, the idea is that the properties are set automatically
without using the constructor, as it the example given.
> What you want to do, representing [AttachData(Type =
> typeof(Foo.Bar))], is setting a value through a property of a custom
> attribute.
>
> You can do it with something like:
>
> var module = ... as ModuleDefinition;
>
> var attachData = new CustomAttribute (module.Import
> (theAttachDataConstructor));
> var value = module.Import (fooBar); // create a reference to Foo.Bar
> scoped for module
I'm not trying to add a new Attribute, I'm trying to modify an
existing attribute in the assembly (as per example).
> var type = module.Import (typeof (Type)); // create a reference to
> System.Type scoped for module
> /*
> the issue with using typeof (Type) is that it will create a reference
> to the version of System.Type your CLR is running on
> so if you're instrumenting a 2.0 assembly from a 4.0 program, you
> better pass a Cecil representation of the 2.0 System.Type
> */
Further to previous question, I get the type by resolving the
module.TypeSystem.CorLib to an AssemblyDefinition and using GetType on
the MainModule, this guarantees I get the type from the right
framework library and avoid creating a dependency on the Type in the
framework currently being used.
> attachData.Properties.Add (new CustomAttributeNamedArgument ("Type",
> new CustomAttributeArgument (type, value)));
Which is fine if adding a new attribute, however I'm trying to modify
an existing attribute. Properties.Add works fine but has no effect on
the resulting assembly (I tried with both Fields & Properties, as I
want to support both).
To come back to my problem, is the only way to remove the existing
attribute and add a new one, or is it possible to do this on an
existing attribute?
If I have to remove the attribute first, how would I do that?
Obviously I'm hoping it is possible to do with the existing attribute.
> The code is not guaranteed to be exact and is written from memory.
>
> Jb
Thanks for your quick response!
--
--
mono-cecil