Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get attribute value for an assembly in Cecil

Is there a way to get str1 in code ?

[MyAttribute("str1")]
class X {}

The instance of Mono.Cecil.CustomAttribute.Fields is empty.

like image 714
Kumar Avatar asked Dec 09 '11 21:12

Kumar


1 Answers

When using attributes in .NET you are either using the constructor parameters and setting some (named) fields. This is encoded differently in the metadata and ends up separately in Cecil.

the instance of Mono.Cecil.CustomAttribute.Fields is empty

What you're using is looking for fields when the constructor arguments were used for the custom attribute. So what you're looking for is:

type.CustomAttributes[0].ConstructorArguments[0].Value
like image 163
poupou Avatar answered Oct 23 '22 19:10

poupou