Hiding the internal interface in the friend assembly

I have two assemblies: A and B. I have InternalsVisibleTo for B. I would like to make calls from A to get information that can only be known by the type defined in B, so that internal things are stored. I can do this using the internal interface defined in and explicitly implemented 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 problem arises when someone refers to assembly B and receives from ClassB - they get an error: "The type" AssemblyA.IHasData "is defined in the assembly that is not referenced," although this type should be invisible to them. If I look at the definition of an open type, I see what I expect - ClassB without interfaces.

? B. IHasData ClassB, A. - , ?

+3
2

, , , , - . .

IHasData , Assembly B . - :

  public abstract class ClassB
  {
    class HasData : IHasData
    {
      HasData(ClassB b) {m_b = b;}
      Data IHasData.GetData() { m_b.GetData(); }
    }

    private readonly HasData m_hasData;

    public ClassB() {
      m_hasData = new HasData(this);
    }

    internal Data GetData() { 
      /** do something internal **/ 
    }
  }
+3

ClassB , - . , ClassB , IHasData, InternalsVisibleTo .

ClassB IHasData, , .

0

Source: https://habr.com/ru/post/1747469/


All Articles