12

Is there a way to get str1 in code ?

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

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

poupou
  • 43,413
  • 6
  • 77
  • 174
Kumar
  • 10,997
  • 13
  • 84
  • 134

1 Answers1

19

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
poupou
  • 43,413
  • 6
  • 77
  • 174