Question

I have a DLL which I have included in my C# project. Let's call it "one.dll" This DLL contain a static class named "staticclass"

I have another DLL which I have also included in same project. Let's call it "two.dll" This DLL also contain a static class named "staticclass"

Now when I include both DLLs at the same time in my project and try to access "staticclass" then naturally it gives error. Is there a way I can change the name of class or give it some kind of alias so let's say "staticclass" in "one.dll" will remain as it is, and I can give alias to "staticclassTwo" which is in "two.dll"

Please note I do not have access to source codec of both "one.dll" and "two.dll"

Was it helpful?

Solution

(I'm assuming the two classes are also in the same namespace. If they're not, it's easy - just use simple using directives for aliases, or the fully qualified name in the code.)

You can indeed give an alias - an extern alias. Effectively this adds "assembly" as another level of namespace differentiation.

Obviously you should avoid this situation when you can, but it's nice that C# provides a way of being very explicit when you absolutely have to.

Anson Horton has a good walkthrough for how you use them in practice.

OTHER TIPS

You can do it by simply using Alias.

In your code, just below to the namespace line; use alias as given below:

namespace ConsoleApp
{

    using ClassOne = Assembly.One.MyClass; /* your dll 1 class */
    using ClassTwo = Assembly.Two.MyClass; /* your dll 2 class */

    class Program
    {

        static void Main(string[] args)
        {

            ClassOne one = new ClassOne();
            // Do your stuff with ClassOne object

            ClassTwo two = new ClassTwo();
            // Do your stuff with ClassTwo object

        }
    }
}

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top