How to get from backend in another assembly

I am new to C #, so please bear with me.

OK, so I have two classes in different assemblies that need to refer to each other:

namespace AssemblyA
{
  class A 
  {
    private B MyB { get; set; }
  }
}

namespace AssemblyB
{
  class B
  {
    private A MyA { get; set; }
  }
}

      

I understand that circular references are not allowed, so I am using the interface:

namespace AssemblyA
{
  public interface IB
  {
     // whatever 'A' needs of 'B'
  }

  class A 
  {
    private IB MyB { get; set; }
  }
}

namespace AssemblyB
{
  class B : AssemblyA.IB
  {
    private A MyA { get; set; }
  }
}

      

This works, but has the disadvantage that it exposes IB

the rest of the world. Instead, I would like to do IB

internal

. But then B

it cannot flow from it.

In C ++ I would make a B

friend and do. I understand that C # has no friends (no pun intended, but noted), so I have to do without it. I read that there is an attribute for this, but that will make the entire assembly A

available to the entire assembly B

, which I don't like. Is there a way to avoid this?

+2


source to share


3 answers


It looks like the big problem here is letting assembly B see one specific member of assembly A.

This denies, according to comments repeating part of the original question, the use of a well-documented attribute InternalsVisibleTo

.

Or that?



Have you considered creating a new C assembly IB

with a tagged interface internal

and its attributes InternalsVisibleTo

for A and B?

This at least exposes IB in a controlled manner without exposing all ABs. I'm not a big fan of the solution (I personally will just keep using InternalsVisibleTo

on A as suggested, then document the rest of my internals to keep others in line), but I understand where you came from - and that is at least solves the problem.

+4


source


In fact, you were misinformed - C # /. NET does support assembly support. You want to mark your two assemblies as Friends Gatherings , which MSDN defines as the following:

An internal type or internal element in an assembly can be obtained from another assembly.



So, just put the following attribute in any of your code files in your project (I would choose AssemblyInfo.cs

).

[assembly:InternalsVisibleTo("name_of_friend_assembly")]

      

+8


source


+1


source







All Articles