使用 PostSharp 将属性应用到界面

发布于 2024-08-23 22:37:26 字数 566 浏览 5 评论 0原文

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

我假设它看起来像这样:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱花细雨 2024-08-30 22:37:26

你必须使用:

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

或:

[Test(AttributeInheritance=MulticastInheritance.Multicast] 
public interface ISystemService 

You have to use:

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

Or:

[Test(AttributeInheritance=MulticastInheritance.Multicast] 
public interface ISystemService 
涙—继续流 2024-08-30 22:37:26

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

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

我相信你应该从一个类继承。


此外,您可以多播属性< /a>,但需要继承自多播属性

What am i missing/doing wrong?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文