Pergunta

Estou usando o Cecil para tentar ler minhas propriedades de atributos:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class TraceMethodAttribute : Attribute {
    public TraceMethodAttribute() {
        MethodStart = true;
        MethodReturn = true;
        MethodMessages = true;
    }

    public bool MethodStart { get; set; }
    public bool MethodReturn { get; set; }
    public bool MethodMessages { get; set; }
}

[TraceMethod(MethodMessages = false)]
static void Main(string[] args) {
}

...

if (attribute.Constructor.DeclaringType.FullName == typeof(TraceMethodAttribute).FullName) {         
  if ((bool)attribute.Fields["MethodMessages"] == true) {
        EditMethodStart(assembly, method);
  }

Isto é, eu gostaria deste último bloco de código para verificar sempre que o atributo aplicado ao Main, por exemplo, possui MethodMessages definido como true ou falso. Pelo que vi, parece que ambos atributos.fields.count e atributes.properties.count está definido como 0. Por que é?

Obrigado

Foi útil?

Solução

Deve funcionar bem através do acesso à coleção de propriedades pelo indexador.

if (attribute.Constructor.DeclaringType.FullName == typeof(TraceMethodAttribute).FullName) {         
  if ((bool)attribute.Properties["MethodMessages"] == true) {
        EditMethodStart(assembly, method);
  }

Apenas compilou e verifiquei.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top