使用 PostSharp 将属性应用到界面
我希望能够将属性应用于接口,以便实现该接口的任何类中的每个方法都将应用该属性。
我假设它看起来像这样:
[Serializable]
[AttributeUsage(AttributeTargets.All, Inherited = true)]
public sealed class TestAttribute : OnMethodBoundaryAspect
{
...
}
然而,当我将它应用到如下所示的接口时,当在实现接口的类中调用该方法时,永远不会访问属性中的 OnEntry/OnExit 代码:
[Test]
public interface ISystemService
{
List<AssemblyInfo> GetAssemblyInfo();
}
如果我在实现类本身,如下所示,它工作正常:
[Test]
public class SystemService : ISystemService
{
...
}
我错过了什么/做错了什么?
I want to be able to apply an attribute to an interface so that every method in any class that implements that interface will have the attribute applied to it.
I assumed it would look something like this:
[Serializable]
[AttributeUsage(AttributeTargets.All, Inherited = true)]
public sealed class TestAttribute : OnMethodBoundaryAspect
{
...
}
Yet when i apply it to an interface like below, the OnEntry/OnExit code in the attribute is never accessed when the method is called in the class implementing the interface:
[Test]
public interface ISystemService
{
List<AssemblyInfo> GetAssemblyInfo();
}
If i apply the attribute within the implementing class itself, as below, it works fine:
[Test]
public class SystemService : ISystemService
{
...
}
What am i missing/doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你必须使用:
或:
You have to use:
Or:
接口没有实现,因此无法执行任何“OnEntry/OnExit 代码”。
我相信你应该从一个类继承。
此外,您可以多播属性< /a>,但需要继承自多播属性。
interface has no implementation, thus cannot execute any ' OnEntry/OnExit code'.
I believe you should inherit from a class.
Additionally you can Multicast the attribute, but you need to inherit from MulticastAttribute.