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

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

问题描述

我有一个包含服务合同的程序集(程序集名称为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;
    }
}

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

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