3

Can I only make some methods visible to the end user when I'm publishing a DLL to third party applications?

My code is built upon 7-8 different projects which call each other, they have different namespaces like "Company.ProjectName" which I think relate under the "Company" namespace, and I only want one of the projects (which has an interface defined BTW) to be visible to outer world.

Every project in the solution compiles into DLL's and then I'm combining them using ILASM.

BTW, there are other projects using these in the solution that are not related to this dll.

Edit: will the internal keyword work even if the namespaces are constructed like "CompanyName.Project1", "CompanyName.Project2" ? Will they see each other?

Taha Paksu
  • 15,371
  • 2
  • 44
  • 78

2 Answers2

3

Use internal

See the example below

public class MyPublicClass
{

    public void DoSomething()
    {
        var myInternalClass = new MyInternalClass();
        myInternalClass.DoSomething();
    }
}

internal class MyInternalClass
{

    public void DoSomething()
    {
    }
}

In your DLL, MyPublicClass will be visible to external users - those who reference your DLL.

MyInternalClass will not be visible.

Alex
  • 37,502
  • 51
  • 204
  • 332
3

You don't need to combine them, you just need a friend assembly:

When you are developing a class library and additions to the library are contained in separate assemblies but require access to members in existing assemblies that are marked as Friend (Visual Basic) or internal (C#).
...
Only assemblies that you explicitly specify as friends can access Friend (Visual Basic) or internal (C#) types and members.

The InternalsVisibleTo attribute:

[assembly: InternalsVisibleTo("AssemblyB")]   

helps to lock it down so only the specified assembly can access the internal items.

(In answer to your edit: this is specified at the assembly level, it doesn't matter what the namespace is).

slugster
  • 49,403
  • 14
  • 95
  • 145
  • I've tried that with no luck. I wrote namespaces, classnames, everything to the `AssemblyB` parameter, but it won't work. – Taha Paksu Feb 09 '16 at 10:56
  • You need to show us specifically what didn't work - without that we can only guess what the problem is. Did you try this but still try to combine the assemblies? – slugster Feb 09 '16 at 10:59
  • I wrote it under the namespace, to the class, I moved it up to the namespace then it worked :) Thanks. – Taha Paksu Feb 09 '16 at 11:03