mardi 30 mai 2017

How to check for an Attribute in a MemberInfo from inside another Attribute?

I implemented the Attribute MyValidate to validate requests to WebApi calls. It's been applied on class level and it's working as expected.

Now I need to make exceptions on some methods and I'm intending to create a new Attribute MyNoValidate. The idea is to change MyValidate to check if the method is also marked with MyNoValidate, thus skipping the validation logic itself, but I don't know how to do it.

public class MyValidate : Attribute
{
   private bool ignoreThisMethod = false;
   public MyValidate()
   {
     if(_the_target_method_has_MyNoValidate()) ignoreThisMethod = true;
   }
}

public class MyNoValidate : Attribute
{ /* nothing to implement */ }

[MyValidate]
public class WillBeValidated
{
   public void ThisMethodWillBeValidated_1() {...}
   public void ThisMethodWillBeValidated_2() {...}

   [MyNoValidate]
   public void ThisIsAnExpecialCasThatWillNotBeValidated() {...}
}





Aucun commentaire:

Enregistrer un commentaire