Question

J'utilise Cecil pour essayer de lire mes propriétés attributs:

[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);
  }

Ceci est, je voudrais ce dernier bloc de code pour vérifier chaque fois que l'attribut principal appliqué à, par exemple, a MethodMessages la valeur true ou false. D'après ce que je l'ai vu, il semble que les deux attributes.Fields.Count et attributes.Properties.Count est mis à 0. Pourquoi est-il?

Merci

Était-ce utile?

La solution

devrait fonctionner correctement grâce à l'accès à la collection Propriétés par indexeur.

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

Juste compilé et vérifié.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top