Thursday, November 29, 2007

VB 9 Series - Friend Assemblies

Welcome to the 3rd Article in the Series of VB 9. In this article we'll look at a new feature added to the VB language i.e. InternalsVisibleTo Attribute - 'Friend Assemblies'.

You might have had some problem in the past where you want to share methods of your 'Friend Assembly' to a particular assembly or assemblies. Now in VB 9.0 you have a way! for this you just have to use an assembly level attribute 'InternalsVisibleTo' (can be found under System.Runtime.ComplierServies) which takes the name of the assembly to which you want to share your methods without extension.

To understand this suppose you have made a Class Library Project say 'testFriend' and write the following code for the class:

Friend Class FriendofDemoClass

.......Function SomeFunction() As Integer

..............Return 0

.......End Function

End Class

Now create a 'Console Application' 'Demo' and after adding the refrence of 'testFriend' make an object of 'FriendofDemoClass' when you make an object of this class outside its scope ('Friend' can only be accessed within the scope of its assembly) you get this error.



Now go back to your 'testFriend' Library and add the 'InternalsVisibleTo' attribute which can be found under System.Runtime.CompilarServices.

Imports System.Runtime.CompilarServices

< InternalsVisibleTo.("Demo") > _
Friend Class FriendofDemoClass

.......Function SomeFunction() as Integer

..............Return 0

.......End Function

End Class

Now everything works fine !

1 comment:

AliAmjad said...

Please leave your comments.