代码之家  ›  专栏  ›  技术社区  ›  krisg

使用PostSharp将属性应用于接口

  •  8
  • krisg  · 技术社区  · 16 年前

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

    我以为会是这样的:

    [Serializable]
    [AttributeUsage(AttributeTargets.All, Inherited = true)]
    public sealed class TestAttribute : OnMethodBoundaryAspect
    {
        ...
    }
    

    [Test]
    public interface ISystemService
    {
        List<AssemblyInfo> GetAssemblyInfo();
    }
    

    如果我在实现类本身中应用该属性,如下所示,它可以正常工作:

    [Test]
    public class SystemService : ISystemService
    {
        ...
    }
    

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

    2 回复  |  直到 16 年前
        1
  •  8
  •   Gael Fraiteur    16 年前

    您必须使用:

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

    [Test(AttributeInheritance=MulticastInheritance.Multicast] 
    public interface ISystemService 
    
        2
  •  1
  •   Dmytrii Nagirniak    16 年前

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

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


    另外,你可以 Multicast the attribute ,但你需要继承 MulticastAttribute