Click here to Skip to main content
15,860,844 members
Articles / Programming Languages / C#
Tip/Trick

Making Internal Access Specifiers Available to Other Assemblies

Rate me:
Please Sign up or sign in to vote.
4.68/5 (6 votes)
4 Oct 2013CPOL2 min read 10.5K   95   9  
How to make internal access specifiers available to other assemblies

Introduction

I was quite interested while reading the article about how the methods and properties with the access specifiers as internal in other assembly/binaries can be accessed. The purpose of the access specifier internal is to have the access level only within the same Assembly in which they are created. But still, is it possible to access the types and members with internal scope in some other assembly? The answer is YES.

We have the attribute named InternalsVisibleTo, which makes the solution for accessing internal types and members of some other assembly easier.

InternalsVisibleTo Attribute

The types and members with the access specifier internal (C#) or friend scope (Visual Basic) are visible only in the assembly in which they are defined.

The InternalsVisibleTo attribute makes internal types and members visible to the types in a specified assembly, which is known as a friend assembly.

The attribute is applied at the assembly level. This means that it can be included at the beginning of a source code file, or it can be included in the AssemblyInfo file in a Visual Studio project.

How to Specify InternalsVisibleTo Attribute

We can use the InternalsVisibleTo attribute to specify a single friend assembly that can access the internal types and members of the current assembly as mentioned below:

C#
[assembly: InternalsVisibleTo("Assembly1")] 

If we have multiple assemblies, then we can declare multiple friend assemblies in two ways as explained below:

C#
//Way:1
[assembly: InternalsVisibleTo("Assembly1")]
[assembly: InternalsVisibleTo("Assembly2")]

 //Way:2
[assembly: InternalsVisibleTo("Assembly1"),
           InternalsVisibleTo("Assembly2")]

Points to Remember

  • We need to add the namespace System.Runtime.CompilerServices to define InternalsVisibleTo attribute.
  • Both the current assembly and the friend assembly must be unsigned, or both assemblies must be signed with a strong name.
  • If they are signed with a strong name, the argument to the InternalsVisibleTo Attribute constructor must include the full public key as well as the name of the assembly.
C#
[assembly: InternalsVisibleTo ("Assembly1, PublicKey=002400000480000094" +
	"0000000602000000240000525341310004000" +
	"001000100bf8c25fcd44838d87e245ab35bf7" +
	"3ba2615707feea295709559b3de903fb95a93" +
	"3d2729967c3184a97d7b84c7547cd87e435b5" +
	"6bdf8621bcb62b59c00c88bd83aa62c4fcdd4" +
	"712da72eec2533dc00f8529c3a0bbb4103282" +
	"f0d894d5f34e9f0103c473dce9f4b457a5dee" +
	"fd8f920d8681ed6dfcb0a81e96bd9b176525a" +
	"26e0b3")]

Code snippets from the sample:

C#
// Now, Assembly1 is a friend of Assembly2.
// Hence, the types and Members with internal scope in Assembly2 will be visible in Assembly1.
[assembly: InternalsVisibleTo("Assembly1")]
namespace Assembly2
{
    public class ClassB
    {
        internal static void Method1()
        {
            Console.WriteLine("Method1() in ClassB is called.");
        }

        internal static string GetName()
        {
            return ClassC.Name; // Accessing the INTERNAL property from ClassC.
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --