Hiding an internal interface in a "friend" assembly

Posted by dmo on Stack Overflow See other posts from Stack Overflow or by dmo
Published on 2010-05-28T02:50:13Z Indexed on 2010/05/28 3:11 UTC
Read the original article Hit count: 232

Filed under:
|

I have two assemblies: A and B. A has InternalsVisibleTo set for B. I would like to make calls from A to get information that can only be known by a type defined in B in a way that keeps things internal. I can do this using an internal interface defined in A and implemented explicitly in B.

Assembly A

  internal interface IHasData
  {
    Data GetData();
  }

  class ClassA 
  {
    DoSomething(IHasData);
  }

Assembly B

  public abstract class ClassB : IHasData
  {
    Data IHasData.GetData() { /** do something internal **/ }
  }

The trouble comes when someone references assembly B and derives from ClassB - they get the error: "The type 'AssemblyA.IHasData' is defined in an assembly that is not referenced" even though that type should be invisible to them. If I look at the public type definition I see what I expect - ClassB with no interfaces implemented.

Why do I get this error? All of the implementation is in assembly B. I could use IHasData internally in ClassB and that wouldn't require assembly A to be referenced. Can someone help me understand what is going on?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET