接口方法的 Postsharp 编译时验证 [英] Postsharp compile-time validation on interface methods

查看:21
本文介绍了接口方法的 Postsharp 编译时验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含服务合同的程序集(程序集名称是 Contracts).我想使用属性和 PostSharp 对这些方法实施授权.授权属性如下所示:

I have an assembly that contains service contracts(assembly name is Contracts). I want to implement authorization on those methods using attributes and PostSharp. The authorization attribute looks like this:

public class Auth : System.Attribute 
{
    public Auth(String permission){...}
}

我希望我的服务合同看起来像这样:

I want my service contracts to look like this:

namespace Contracts
{
    public interface IService
    {
        [Auth("CanCallFoo")]
        void Foo();
    }
}


我想在编译时检查 Contracts 程序集中接口中的所有方法是否具有 Auth 属性.
为了做到这一点,我创建了以下方面:


I want to check at compile-time that all the methods from the interfaces in the Contracts assembly have an Auth attribute.
In order to do this I've created the following aspect:

[Serializable]
[MulticastAttributeUsage(MulticastTargets.Interface & MulticastTargets.Method)]
public class EnforceSecurityAspect : OnMethodBoundaryAspect
{
    public override bool CompileTimeValidate(System.Reflection.MethodBase method)
    {
        var hasSecurityAttribute = method.GetCustomAttributes(true).Any(x => x is Auth);
        if (!hasSecurityAttribute)
        {
            throw new InvalidAnnotationException(String.Format("Add `Auth` to `{0}`", method.Name));
        }

        return base.CompileTimeValidate(method);
    }
}

我在Contracts程序集的AssemblyInfo中使用这行代码应用方面:

I apply the aspect using this line of code in AssemblyInfo of Contracts assembly:

[assembly: EnforceSecurityAspect()]

在同一个程序集中,我还有 DTO,供服务使用.
我面临的问题是方面也适用于 DTO
例如我有一个这样的 DTO

Within the same assembly I also have DTOs, which are used by the services.
The problem I am facing is that the aspect gets also applied to DTOs
For example I have a DTO like this

public class Client
{
    public String Name{get;set;}
}

并且在编译时我收到一个错误,提示我应该将 Auth 添加到编译器生成的 get_Name 方法中.
问:有没有办法告诉 Postsharp 切面应该只应用于接口的方法?

and at compile-time i get an error that says that I should add Auth to the compiler-generated get_Name method.
Q: Is there a way to tell Postsharp that the aspect should be applied only to methods of interfaces?

推荐答案

我知道这是一个老问题,但我也想为遇到此问题的任何人提供一个答案.

I know this is an old question but I wanted to provide an anwser to anyone having this issue as well.

虽然可以使用特定目标继承/实现某个接口/类,但我不知道如何使用 postsharp.

While it may be possible to use specific targets inheriting/implementing a certain interface/class with postsharp i dont't know how.

对此的解决方案是创建一个方面并以类为目标(还没有这样做,但我确定它可能).然后,您可以使用反射仅验证从某个类/接口继承的类使用 输入,例如FindInterfaces

the solution to this is to create an aspect and target the classes (haven't dont this yet, but im sure its possible). you can then use reflection to validate only the classes that inherit from a certain class/interface using methods available on Type such as FindInterfaces

在下面的代码中使用一些类似的东西.(注意这是一个 postsharp Enterprice 功能)

use something simmilat to the below code. (Note this is a postsharp Enterprice feature)

[MulticastAttributeUsage(MulticastTargets.Class)]
public class MulticastTest : MulticastAttribute, IScalarConstraint
{
    public void ValidateCode(object target)
    {
        throw new NotImplementedException();
    }

    public bool ValidateConstraint(object target)
    {
        var type = target.GetType();
        Console.Write(type.Name);//Actually do something here
        return true;
    }
}

这篇关于接口方法的 Postsharp 编译时验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆