Friday, December 5, 2014

Step By Step : InternalsVisibleTo attribute usage


Hi,

Welcome to my blog. In this post we will see how we can use InternalsVisibleTo attribute in your assembly and make the types that are ordinarily visible only within the current assembly are visible to a specified assembly. I had a method called "Add" in my class called "MathsDao.cs" and I need to use it in my unit test class called "MathsUnitTest.cs". I donot want to make my Add method public. I did some research and came across "InternalsVisibleTo" that helps me to expose my MathsDao.cs library to my unit test classes library. Lets see how I did it with a step by step example.

This is just one of the way which I followed  I am not saying this is the best and it doesnot have cons. My main aim is to share my experience so that it will be useful for the needy. I am new to blogging and I tried to make this post as much interesting as possible. Please excuse me for my grammatical errors and typos. I welcome your constructive comments and suggestions which helps in improving this post and my technical skills. 

Overview:
In this example we will see how we can expose a type in one assembly to another specified assembly. We will create the following projects

1) DataAccessObjects : This is C# class library that has MathsDao.cs with one internal method called Add.
2) DaoUnitTest: This is C# unit test project that has MathsDaoUnitTest.cs with one method called UT_Add. This will test the internal method Add in MathsDao.cs

Now let's start our work
For this article, I am using the following tools/technologies to develop a my sample application
Visual Studio 2013
C#
.NET Framework 4.5

I always have the habit of creating empty solution first and then work on it. Here also I did same thing. I created an empty visual studio solution titled "InternalsVisibleToDemo

I added a new C# class library project called "DataAccessObjects" and added "MathsDao.cs" to it.
I added a new C# unit test project called "DaoUnitTest" and added "MathsDaoUnitTest.cs" to it.



















 Now let's add below code to our MathsDao.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessObjects
{
    public class MathsDao
    {
        private int Add(int a, int b)
        {
            return a + b;
        }
    }
}
 

 Build solution and add DataAccessObjects.dll to DaoUnitTest project

















Open MathsDaoUnitTest and add below code

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using DataAccessObjects;

namespace DaoUnitTest
{
    [TestClass]
    public class MathsDaoUnitTest
    {
        [TestMethod]
        public void UT_Add()
        {
            MathsDao dao = new MathsDao();
           
        }
    }
}


If you try to access Add inside MathsDao you can see it is not available.















This is our problem how we can expose Add from MathsDao.cs into MathsUnitTest.cs without making it public. The answer to this is we can done by using InternalsVisibleTo.

Why we need InternalsVisibleTo?
Using this attribute we can successfully share the components which would otherwise be hidden with another assembly as we choose. The sharing of the components can be done by performing below two steps

1) Make your private methods as internal methods
2) Add InternalsVisibleTo attribute to the class whose internal types and methods you want to share with other assemblies.

Internal methods:
As I said in above steps, we need to convert our private methods to internal. 

Open MathsDao.cs and change Add method like below
 
internal int Add(int a, int b)
        {
            return a + b;
        }



InternalsVisibleTo attribute: 

As per Microsoft's MSDN : This attribute is used to identify one or more friend assemblies for a given assembly. A friend assembly is an assembly that can access another assembly's internal types and members. If you identify an assembly as a friend assembly, you no longer have to mark types and members as public in order for them to be accessed by other assemblies

Open MathDao.cs and add change code as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

[assembly: InternalsVisibleTo("DaoUnitTest")]
namespace DataAccessObjects
{
    public class MathsDao
    {
        internal int Add(int a, int b)
        {
            return a + b;
        }
    }
}


assembly: InternalsVisibleTo(the assembly namespace which needs to have access to internal methods of MathsDao.cs)

Build your solution.

Now go to MathsDaoUnitTest.cs and you will see now you have access to Add method





















Conclusion :
In this article we
saw how we can use InternalsVisibleTo attribute in your assembly and make the types that are ordinarily visible only within the current assembly are visible to a specified assembly. We created a simple application and saw step by step how a non public method is one assembly can be accessed in other assembly.

I hope you enjoyed reading this post as much as I did while writing it. Please feel free to let me know your valuable and constructive comments and suggestions which can help me to make this article better and improve my technical and written skills. Last but not the least please excuse me for my grammar and typos.

Thanks and have a nice and wonderful day.

No comments:

Post a Comment

Please add your comments. Please avoid personal attacks and racist comments. Your constructive comments are always welcome. I will try my best to address your comments in time.