C# 使用PostSharp将属性应用于接口

C# 使用PostSharp将属性应用于接口,c#,interface,attributes,postsharp,C#,Interface,Attributes,Postsharp,我希望能够将属性应用于接口,以便实现该接口的任何类中的每个方法都将该属性应用于该接口 我以为它会像这样: [Serializable] [AttributeUsage(AttributeTargets.All, Inherited = true)] public sealed class TestAttribute : OnMethodBoundaryAspect { ... } 但是,当我将其应用于下面这样的接口时,当在实现接口的类中调用该方法时,属性中的OnEntry/OnExit

我希望能够将属性应用于接口,以便实现该接口的任何类中的每个方法都将该属性应用于该接口

我以为它会像这样:

[Serializable]
[AttributeUsage(AttributeTargets.All, Inherited = true)]
public sealed class TestAttribute : OnMethodBoundaryAspect
{
    ...
}
但是,当我将其应用于下面这样的接口时,当在实现接口的类中调用该方法时,属性中的OnEntry/OnExit代码永远不会被访问:

[Test]
public interface ISystemService
{
    List<AssemblyInfo> GetAssemblyInfo();
}
我错过了什么/做错了什么

我错过了什么/做错了什么

接口没有实现,因此无法执行任何“onetry/OnExit代码”

我认为你应该继承一个阶级


此外,您可以,但您需要从继承。

您必须使用:

[MulticastAttributeUsage(..., Inheritance=MulticastInheritance.Multicast)]
public sealed class TestAttribute : OnMethodBoundaryAspect 
或:


引用PostSharp文档:“您可以在接口上放置一个自定义属性,并将其隐式应用于实现该接口的所有类。”因此,如果我将其应用于该类,并将其应用于其中的所有方法/属性,那么根据上述语句,将其应用于接口也应该这样做。对吗?这适用于“自定义属性多播”。我在答案中提供了链接。@Dmitri,链接断开了。你的意思是什么?在回答的时候,链接起作用了。请随时用新链接更新我的答案。我已经不记得了:)
[MulticastAttributeUsage(..., Inheritance=MulticastInheritance.Multicast)]
public sealed class TestAttribute : OnMethodBoundaryAspect 
[Test(AttributeInheritance=MulticastInheritance.Multicast] 
public interface ISystemService