-1

After this: Custom Attribute not being hit I'm looking for way to hit method's attributes in my application.

Currently as I know if there's some custom attribute like

 [AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute
{
    public MyAttribute()
    {
        Console.WriteLine("Hello");
    }
}

And method that decorated with MyAtrtribute like:

[MyValidation]
public class Service
{
    [MyValidation]
    public bool GetService(int id)
    {
        if (id > 100)
        {
            return true;
        }
        return false;
    }
}

The way to fire this attribute is by calling method service.GetType().GetCustomAttributes(false). The question is: If I want to decorate many methods in many classes what the way to apply the attributes? In my case I want to apply license validation when getting method invokes like createProject(), createUser() etc.

The way like this codes service.GetType().GetCustomAttributes(false) seems too cumbersome. I want some central location that will be responsible for firing the attributes applying. There is some elegant solution for it? Thanks!

Community
  • 1
  • 1
Roni
  • 369
  • 1
  • 7
  • 22

1 Answers1

1

What you want is AOP (aspect oriented programming) because right now, if you need to manually call the attributes yourself, they are worthless for that purpose. You could as well call the method instead of putting it into an Attribute.

Google AOP and C# and you will find a few providers.

The most impressive probably is PostSharp. But it's also the one that costs real money. Others are free.

nvoigt
  • 75,013
  • 26
  • 93
  • 142