Search...

Saturday, April 27, 2013

Friend Assemblies in C#


Occasionally you may want to access internal classes and their members found in one assembly from code in a separate assembly. The 'internal' modifier prevents this type of access. This restriction can be circumvented using C# 2.0 friend assemblies.

The Internal Modifier


The 'internal' access modifier can be applied to classes and their members to restrict their visibility to external objects. Internal items are available outside of the class but only to other items within the same assembly. Other assemblies are unaware of the internal elements. Sometimes you may wish to remove this restriction, allowing the internal classes and members in one assembly to be visible to other, specific assemblies. You may want to do this to split a large assembly into more manageable parts or to allow testing of internal items from a separate automated testing assembly. This can be achieved using friend assemblies.

NOTE: The use of friend assemblies is good for testing purposes. If you use friend assemblies for other purposes, you should consider your design to ensure that you are not breaking the rules of encapsulation.

Creating Friend Assemblies


Friend assemblies are created by adding an assembly attribute to the code. This attribute specifies the name of another assembly that will be granted access to internal members as if they were public. Any assemblies not specified will be prevented from accessing internal items. Private and protected classes and members always remain inaccessible.

To demonstrate, create a new, blank solution in Visual Studio named "FriendAssemblyDemo". Add two projects to this solution. The first should be a console application named "Caller" and the second should be a class library named "InternalsVisible". The Caller application will access an internal class in the InternalsVisible assembly. To link the two assemblies, add a reference to the class library within the console application.

We can now add an internal class to the InternalsVisible project. Modify the automatically created class file as follows:

Modify the Program class in the console application as shown below:
class Program
{
    static void Main(string[] args)
    {
        InternalsVisible.World world = new InternalsVisible.World();
        world.ShowMessage();
    }
}

If you attempt to compile the solution, you will see a compiler error. This error indicates that the InternalsVisible.World class cannot be accessed due to its protection level. To specify that the internals of the InternalsVisible project should be available to the Caller application, we need to add the following attribute to the class library's assembly. This line should appear outside of any namespace or class declaration.

NOTE: This attribute is found in the System.Runtime.CompilerServices namespace so ensure that the code file in which it appears includes the line, using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Caller")]

You should now be able to compile and execute the solution.

Working with Signed Assemblies


If the assembly that contains the internal items is signed with a public / private key pair, creating friend assemblies is more complicated. Firstly, you must sign any assemblies that access the signed code's internal members. Secondly, the InternalsVisibleTo attribute must be modified to include the public key, which is over three hundred characters in length.

 For example:
[assembly: InternalsVisibleTo("Caller, PublicKey=0024000004800000940000000602000000240000
525341310004000001000100655f087992abe9baf3d84d7bd066c98064b68633c1a7e1777d0e6c549c6c105a2
d9f03a0ca8076b42299b9da111e3e8efc5a02329959804e070e868b27263b7b5607d799553709fe4bb9d0d07c
e13b548b01dedf9afd033dda8e4a81639123160e7eb115cc0d16cb83c42b91affb029f846b07d86ec79b6b289
5b337f01d12b3")]

NOTE: The key must appear on a single line.

The public key can be obtained using the strong name tool in a two-stage process. Firstly, the public key must be extracted from the key file into a new file. This is achieved using the "p" switch. For example, if the key file is named "Caller.snk", you can copy the public key to a new file named "public.key" from the Visual Studio command line using the following command:

sn.exe -p Caller.snk public.key

The second stage is to view the contents of the new public key file in hexadecimal format. This can be outputted using the following command. The key value should then be copied and pasted into the assembly attribute.

sn.exe -tp public.key

No comments: