Search code examples
c#.netnamespacesdependenciesproject

Hide namespace in 3rd dependent project


I Have 3 projects in same solution, each dependent from the one before (DB <- Provider <- WinUI). In DB I defined a Behaviour class that will be used in Providers with a mapper Behavioud class, this last used by WinUI project. (WinUI doesn't depend directly from DB).

Is there a way to hide the clases from DB inside WinUI? this project only should use the provider clases.

enter image description here

enter image description here


Solution

  • First, mark the Behaviour class in the DB project as internal.

    Second, add

    • for a project that contains an AssemblyInfo.cs file
      [assembly: InternalsVisibleTo("Providers")]
      
    • for .Net 5+, in the csproj file (more info)
      <ItemGroup>
        <InternalsVisibleTo Include="Providers" />
      </ItemGroup>
      

    internal prevents the class from being visible in other assemblies. The assembly attribute allows sharing internal classes with the assemblies you name.