加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 仅将方面应用于具有特定属性的方法

发布时间:2020-12-15 08:40:50 所属栏目:百科 来源:网络整理
导读:我正在尝试设置PostSharp方面RunOutOfProcessAttribute,以便它适用于: 所有公共方法 任何标有DoSpecialFunctionAttribute的方法,无论成员可访问性如何(public / protected / private / whatever). 到目前为止,我的RunOutOfProcessAttribute是这样定义的: [
我正在尝试设置PostSharp方面RunOutOfProcessAttribute,以便它适用于:

>所有公共方法
>任何标有DoSpecialFunctionAttribute的方法,无论成员可访问性如何(public / protected / private / whatever).

到目前为止,我的RunOutOfProcessAttribute是这样定义的:

[Serializable]
[MulticastAttributeUsage(MulticastTargets.Method,TargetMemberAttributes = MulticastAttributes.Public)]
[AttributeUsage(AttributeTargets.Class)]
public class RunOutOfProcessAttribute : MethodInterceptionAspect
{
    public override void OnInvoke(MethodInterceptionArgs args)
    {
        ...
    }
}

已经存在的MulticastAttributeUsageAttribute应该满足上面的标准1,但是我不知道如何满足标准2,而不是简单地将现有方面的行为复制到新属性中.

如何将此方面应用于标记有DoSpecialFunctionAttribute的任何方法,无论成员可访问性如何(public / protected / private / whatever)?

解决方法

这是解决方案:

>使用[MulticastAttributeUsage(MulticastTargets.Method)]定位所有方法
>覆盖CompileTimeValidate(MethodBase方法).设置返回值,使得CompileTimeValidate在适当的目标上返回true,在目标上为false以静默忽略,并在应警告用户Aspect使用不合适时抛出异常(这在PostSharp documentation中详述).

在代码中:

[Serializable]
[MulticastAttributeUsage(MulticastTargets.Method)]
[AttributeUsage(AttributeTargets.Class)]
public class RunOutOfProcessAttribute : MethodInterceptionAspect
{
    protected static bool IsOutOfProcess;

    public override void OnInvoke(MethodInterceptionArgs args)
    {
        ...
    }

    public override bool CompileTimeValidate(MethodBase method)
    {
        if (method.DeclaringType.GetInterface("IDisposable") == null)
            throw new InvalidAnnotationException("Class must implement IDisposable " + method.DeclaringType);

        if (!method.Attributes.HasFlag(MethodAttributes.Public) && //if method is not public
            !MethodMarkedWith(method,typeof(InitializerAttribute))) //method is not initialiser
            return false; //silently ignore.

        return true;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读