【问题标题】:Is there any way to add attributes to methods of inherit interface?有没有办法为继承接口的方法添加属性?
【发布时间】:2017-01-19 15:17:22
【问题描述】:

有多种技术实现的通用服务接口。

例如,我有2个接口:

  1. IGenralService
  2. 从 IGenralService 继承的 IWcfService。

基本接口:

    public interface IGenralService
    {
         bool Login(string username, string password);
    }

还有 wcf 服务:

public interface IWcfService : IGenralService
{
    [OperationContract(IsOneWay = false)]
    [FaultContract(typeof(Exception))]
    void DoSomething();
}

IWcfService 特定于 Wcf,并且需要 wcf 方法的“OperationContract”属性。 “Login”方法不包含“OperationContract”属性。

有没有办法给固有方法添加属性?

【问题讨论】:

  • 您希望IWcfServiceLogin 方法用属性修饰,而不是IGeneralService?这基本上是无意义的,因为任何实现您的任何接口的类都不会继承其属性,您必须重新声明它们。所以无论如何你都可以在界面中省略它们。

标签: c# wcf inheritance interface operationcontract


【解决方案1】:

我想这是不可能的,因为属性不会继承到实现接口的类。所以基本上向接口成员添加属性是没有用的,你必须在类本身上这样做:

public class WcfService : IWcfService 
{
    [OperationContract(IsOneWay = false)]
    [FaultContract(typeof(Exception))]
    void DoSomething() { ... }
}

另外,您还可以使您的接口成为一个抽象类,从而您可以继承属性。请查看this post 了解如何执行此操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 2019-10-24
    • 2011-03-20
    • 1970-01-01
    • 2011-02-16
    相关资源
    最近更新 更多