0

Here's how I want to use them:

class SecuredModel
{
    public SecuredModel() { }

    [Permission(Permissions.Read)]
    public void restrictedMethod()
    {
        if (IsPermitted)
        {
            // code
        }
    }
}

I have defined here the "Permission" class:

[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)]
class Permission : System.Attribute
{
    private Permissions PermissionsRequired { get; set; }

    public bool IsPermitted
    {
        // some code to check for permissions
    }

    public Permission(Permissions permissionsRequired)
    {
        this.PermissionsRequired = permissionsRequired;
    }
}

The problem I am having is that I don't know how to use the attributes I've assigned to my methods. I'd like to access them from within the method, is that a possibility? If not, could I instead access them from outside the method? I haven't been able to find this usage of any of the MSDN pages I've looked at, and I've seen some answers on SO, but I feel like a lambda expression is overcomplicating this. It shouldn't be that difficult, right?

Kevin Minehart
  • 482
  • 4
  • 14
  • You can use them within your methods but I don't see a point of that. The point is to allow *other* methods gather information about the object and/or its members. – bokibeg Dec 17 '15 at 21:08
  • You would access them using [attribute reflection](https://msdn.microsoft.com/en-us/library/z919e8tw.aspx) and you'd have to [intercept method calls at runtime](http://stackoverflow.com/questions/25803/how-do-i-intercept-a-method-call-in-c), and use something like [CodeAccessPermission](https://msdn.microsoft.com/en-us/library/system.security.codeaccesspermission.demand%28v=vs.110%29.aspx), but isn't it easier to say `if (IsPermitted())` and use a base class that says yes or no? – Kenney Dec 17 '15 at 21:36
  • That's true, that is far easier. I'm more envisioning a scenario where all that my colleagues will have to do to decide whether or not a method can be executed by a group is simply define it above the method signature, but if the implementation is that ridiculous it would probably be easier to just move all of the permission stuff to SecuredModel and have them inherit SecuredModel and check for permissions themselves. – Kevin Minehart Dec 17 '15 at 21:44
  • The attributes/annotations is a nice approach, and in Java it's very doable. I just found [AOP approach in .Net](https://msdn.microsoft.com/en-us/library/aa288717%28v=vs.71%29.aspx), and it seems this uses runtime CLR code injection, so there's little overhead. – Kenney Dec 17 '15 at 22:09

1 Answers1

1

Attributes are a way to decorate classes but they are only useful when you have some abstraction (like an IDE or some upfront processing mechanism that inspects the class) that is enforcing their purpose. They don't make sense to be used within the method/property they decorate (performance etc..)

Consider adding some extra properties (private/protected) that are set after inspecting the class, that way you are not reflecting all the time.

Here is a helpful link on attributes: Reflection - get attribute name and value on property

Community
  • 1
  • 1
T McKeown
  • 12,971
  • 1
  • 25
  • 32