Is there any way to add attributes to methods of inherit interface?

298 Views Asked by At

There is general service interface that been implemented by multi technologies.

For example, I have 2 interfaces:

  1. IGenralService
  2. IWcfService that inherit from IGenralService.

The base interface:

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

And the wcf service:

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

The IWcfService is specific for Wcf and need "OperationContract" attribute for wcf methods. The "Login" method does not including the attribute "OperationContract".

Is there any way to add attribute to the inherent method?

1

There are 1 best solutions below

0
MakePeaceGreatAgain On

I suppose this is not possible, as attributes are not inherited to classes implementing an interface. So basically adding attributes to interface-members is quite useless, you have to do this on the classes themselves:

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

Alternativly you can also make your interface an abstract class whereby you can inherit attributes. Have a look at this post for how to do this.