JoeDev
JoeDev

Reputation: 105

How to restrict access to an internal interface implemented by other public interface

Using C# and NET 4.0, I've two public Interfaces (IAsyncMethod and ISyncMethod) that implements an internal inteface (IMethod), I need to set public access modifier to the first two, and internal for the third, so I can enable only the two public interfaces for another dll/project/developer.

internal interface IPaymentMethod
{
  XmlDocument StartTransaction(XmlDocument parameters);
  XmlDocument QueryTransaction(XmlDocument parameters);
}

public interface ISyncMethod : IMethod
{
  void EndTransaction(TransactionDTO parameter);
}

public interface IAsyncMethod : IMethod
{
  void EndTransaction(TransactionDTO parameter);
}

When I build the project, I receive these errors:

Inconsistent accessibility: base interface 'IMethod' is less accessible than interface 'ISyncMethod'
Inconsistent accessibility: base interface 'IMethod' is less accessible than interface 'IAsyncMethod'

How I will set the modifiers to allow the behavior that I need?

Upvotes: 2

Views: 407

Answers (2)

Andras Zoltan
Andras Zoltan

Reputation: 42363

All the bases and interfaces of a type must be at least as visible as that type. So an internal/private type/interface can inherit/implement a public one; but not the other way around.

Think about it - only certain types are exported from an assembly; if you, as an assembly consumer, were presented with these types, with the IMethod interface listed as an interface - you would be stumped, because you would expect to find a declaration of that interface somewhere. But since it's internal, you wouldn't.

Equally, what would be the point of visibility modifiers on types/interfaces if they could be subverted by an inheriting/implementing type?

Upvotes: 1

Eugen Rieck
Eugen Rieck

Reputation: 65324

This is not a question of "how to set the modifiers", but a question of inheritance basics: Let's look into (in third-party code)

ISyncMethod sm=new ClassImplementingISyncMethod();
IMethod m=(IMethod) sm;

this is valid, as ISyncMethod inherits from IMethod. This ofcourse is impossible, if IMethod is inaccessible.

Valid but impossible is a combination, the compiler dislikes.

Upvotes: 3

Related Questions