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()
        FriendClass.SayHello()
        Console.ReadLine()
    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.

Regards,
Agung Gugiaji

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s