我正在尝试设置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; } }