Monthly Archives: May 2014

Securing Your App – Making ‘Friend’ Class Available To Another Assembly With InternalsVisibleTo

Friend modifier makes a class and/or procedure only available within same assembly. It’s intended for securing your application so that your Friend class cannot be stealed by unsign assembly.

However, if we want to make your Friend class available to other assembly then we have to use InternalVisibleTo attribute.
InternalVisibleTo attribute sign another assembly so that Friend class could be acessed from that assembly.

I give you a simple example to do that.
Lets make library project FriendClassLib.vbproj. Project’s class source code:

Public Class FriendClass
    Friend Shared Sub SayHello()
        Console.WriteLine("Hello from FriendClass")
    End Sub

    Public Shared Sub PublicHello()
        Console.WriteLine("Public Hello")
    End Sub
End Class

The friend shared procedure, SayHello(), can’t be accessed by outside of this assembly as default. Here I want to make it can be accessed.

  1. Open AssemblyInfo.vb in FriendClassLib.vbproj
  2. Add this line <Assembly: InternalsVisibleTo(“FriendUsage”)>
    This intended to make all friend procedure available to FriendUsage.vbproj assembly

Create new FriendUsage.vbproj Console Project. Module source code:

Imports FriendClassLib
Module Module1

    Sub Main()
    End Sub

End Module

This console project has reference to FriendClassLib. With the use of InternalsVisibleTo attribute in FriendClassLib then the SayHello() can be run from other assembly, FriendUsage.

Agung Gugiaji