title: Passing dynamic parameters in attributes (PostSharp)
body: <p>I'm trying to implement a PostSharp attribute to check whether a user accessing a certain method is authorised to do so. I had implemented a test solution but with hard coded values like so:</p>
  
  <pre><code>[AuthorisationAspect(RolesEnum.Roles.Admin, RolesEnum.Roles.User]
  </code></pre>
  
  <p>First parameter describes which role the user should have, the second parameter describes the role of the current user. Like I've mentioned this was just a test. What I'm trying to implement now is the same concept with a few differences. Both parameters are now lists because a method could be accessed by multiple roles and a single user could also have multiple roles. Therefore what I'm trying to achieve is comparing these two lists using PostSharp. I've tried a few different ways to solve it but I'm always getting the same error:</p>
  
  <blockquote>
    <p>"An attribute argument must be a constant expression , typeof
    expression or array creation expression of an attribute parameter
    type."</p>
  </blockquote>
  
  <p><strong>Solutions I've tried:</strong></p>
  
  <p>First I've tried something like the following, but just like the error describes I can't call methods inside an attribute.</p>
  
  <pre><code>[AuthorisationAspect(GetRoles(), GetUserRoles()]
  </code></pre>
  
  <p>But then I realised that this is not possible as only static/constant values can be passed as parameters in attributes.</p>
  
  <p>I've also tried using something based on this solution. <a href="https://stackoverflow.com/questions/6665187/how-to-set-dynamic-value-in-my-attribute">How to set dynamic value in my Attribute</a> but again it did not even compile.</p>
  
  <p>Finally I've also looked at the following solution <a href="http://geekswithblogs.net/abhijeetp/archive/2009/01/10/dynamic-attributes-in-c.aspx" rel="nofollow noreferrer">http://geekswithblogs.net/abhijeetp/archive/2009/01/10/dynamic-attributes-in-c.aspx</a>, but it looks far too complicated when what I'm trying to do is use AOP which should make things simpler.</p>
  
  <p>Basically I'm trying to pass dynamic parameters in an attribute and passed to a PostSharp attribute but I can't achieve it. I don't know if it's possible, maybe there is a better way to solve this issue. Any help would be appreciated.</p>
  
  <p><strong>Note:</strong> The simplest solution would be to call methods to access the database straight from the PostSharp aspect. However, I can't access these methods from the aspect because referencing the class library where the aspects reside would result in a circular dependency. (I'm using 3-tier architecture)</p>