Thursday, 20 February 2014

multiple DLL with same namespace in single application

Here is an little article for problem:
Problem : If we have two DLL with same Namespace. Then how can we maintain class if we have add this dll in our project, then what happen ?
Let’s see the solution.
We have 1st DLL : Lib1
In this we have following implementation
namespace Lib1
{
    public class Class1
    {
        public void Method()
        {
       
        }
    }
}

We have 2nd DLL : Lib2
In this we have following implementation
namespace Lib1
{
    public class Class1
    {
        public void Test()
        {
       
        }
    }
}


Now, Add reference of this dlls in other project.



Now if we are calling the method of dll from this application then we got following error.










This type of error can be solved using class alias Name like below.




By this you will not face any error. But new question is How to use this alias class. See below.
extern alias TestAlias;
using System;

public partial class Fullscreen : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Lib1.Class1 obj = new Lib1.Class1();
        obj.Method();

        TestAlias::Lib1.Class1 obj1 = new TestAlias::Lib1.Class1();
        obj1.Test();
    }
}
You must need to write  “extern alias”  on the top of the page otherwise .net reflect error.
This way you can use both or more then 2 dlls with same namespace in single application.


No comments:

Post a comment