Closed Muitsonz closed 1 year ago
If you specify "object" as the parameter type, there still needs to be a trace of the actual object type in the assembly.
So the value there needs to be an instance of CustomAttributeArgument
, specifying the actual type (here probably Int32
) plus the value.
For future reference: if you want to know what you need to create, just compile something that includes it and look at how Cecil represents them. Then you know exactly what to create.
Note: for builtin types, it's easier(*) to go via TypeSystem; so module.TypeSystem.Object
and module.TypeSystem.Int32
.
(*) and also safer - otherwise you might accidentally emit a reference to the wrong type; if your code is running in net6.0 and you are working on a net472 assembly, importing a reference to typeof(object)
may cause it to add a reference to the System.Runtime or System.Private.CoreLib instead of to mscorlib
I want to add a custom attribute by cecil, which accepts objects and whose ctor is defined as
MyAttribute(object obj)
. When I tried the following code:System.InvalidCastException
is thrown later that says 'Unable to cast object of type 'System.Int32' to type 'Mono.Cecil.CustomAttributeArgument'.' with the following stacktrace:But if I used the specific data type instead of object on the attribute ctor, my code was able to run as expected. I wonder if the exception is thrown from https://github.com/jbevain/cecil/blob/56d4409b8a0165830565c6e3f96f41bead2c418b/Mono.Cecil/AssemblyWriter.cs#L2976C1-L2976C1, where a casting happens.
Can someone tell me the proper way of adding an object argument to custom attributes?