在屬性中傳遞動態參數 (PostSharp) (Passing dynamic parameters in attributes (PostSharp))


問題描述

在屬性中傳遞動態參數 (PostSharp) (Passing dynamic parameters in attributes (PostSharp))

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:

[AuthorisationAspect(RolesEnum.Roles.Admin, RolesEnum.Roles.User]

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:

  

"An attribute argument must be a constant expression , typeof   expression or array creation expression of an attribute parameter   type."

Solutions I've tried:

First I've tried something like the following, but just like the error describes I can't call methods inside an attribute.

[AuthorisationAspect(GetRoles(), GetUserRoles()]

But then I realised that this is not possible as only static/constant values can be passed as parameters in attributes.

I've also tried using something based on this solution. How to set dynamic value in my Attribute but again it did not even compile.

Finally I've also looked at the following solution http://geekswithblogs.net/abhijeetp/archive/2009/01/10/dynamic‑attributes‑in‑c.aspx, but it looks far too complicated when what I'm trying to do is use AOP which should make things simpler.

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.

Note: 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)


參考解法

方法 1:

There is no good way to use not compile time constants as attribute parameters.

You can consider to attack the other issue with circular dependency with a third dll that contains an abstraction (interface or base abstract class or a static wrapper for your implementation) and reference this in both assemblies and use implementation of that abstraction in you aspect.

The other way I've solved this problem is by enclosing the calls to your methods in constructor of derived attribute class:

class DerivedAuthorisationAspect:AuthorisationAspect
{
   public DerivedAuthorisationAspect():base(sth.GetRoles(), sth.GetUserRoles()){}
}

this will work if you can figure out the sth. note that attribute instances are special kinds of classes so your methods can be called multiple times per class. 

I would advise the first one even if it is more work to do ‑ it is cleaner.

(by Daniel GrimaRafal)

參考文件

  1. Passing dynamic parameters in attributes (PostSharp) (CC BY‑SA 3.0/4.0)

#postsharp #aop #attributes #C#






相關問題

拋出自定義異常並使用 Postsharp 捕獲它們 (Throw an custom exception and catch them with Postsharp)

Làm cách nào để loại trừ khỏi ghi nhật ký dựa trên OnMethodBoundaryAspect? (How to exclude from OnMethodBoundaryAspect-based logging?)

在屬性中傳遞動態參數 (PostSharp) (Passing dynamic parameters in attributes (PostSharp))

PostSharp 3.0 中 IAspectProvider 的重大變化? (Breaking change of IAspectProvider in PostSharp 3.0?)

將方面應用於其他程序集類方法調用 (apply an aspect to other assembly class methods calls)

Postsharp 和 log4net 和 log4postsharp (Postsharp and log4net and log4postsharp)

Postsharp - 獲取調用程序集? (Postsharp - Get Calling Assembly?)

PostSharp 對象映射器 (PostSharp for an object mapper)

PostSharp - 自動化事件訂閱和集合添加 (PostSharp - automate event subscription and collection addition)

PostSharp:在目標構造函數之後初始化實例範圍的方面 (PostSharp: initialize instance-scoped aspect after target constructors)

使用 PostSharp 1.0 的 ClickOnce 應用程序似乎需要 GAC 中的 1.5 程序集 (ClickOnce application that uses PostSharp 1.0 seems to require 1.5 assemblies in GAC)

更改用戶控件 DependencyProperty 時屬性重置 (Property resets when changing Usercontrols DependencyProperty)







留言討論