Reputation: 1
i'm working on a program at the moment, which uses multiple .dll's as reference. It all worked fine, but yesterday i added a new reference and i got an error on an Object i was using from another reference and it said this object is definded in both .dll Files. I tried removing the other reference but i need what is in that reference so i have to work around it somehow. I searched online but i didn't found a good solution as it seems like not many having this problem! Because of that the help of an experienced programmer is needed more than ever, but i appreciate every piece of help of course :)
Upvotes: 0
Views: 59
Reputation: 1038720
It's very bad practice to have the same class defined in the same namespace in 2 different assemblies. You have already found out why. This being said, there's a mechanism which allows to disambiguate the references called external aliases.
Let's suppose that you have a console application which references 2 assemblies that contain the same class Foo.SomeClass
(ClassLibrary1
and ClassLibrary2
). In the project where you want to use the class, select the assembly reference and in the Properties window define an alias (in addition to the default global
alias):
Do the same for the second assembly reference and use a different alias name.
Now you can use the assembly aliases that you provided to the reference in order to specify the which class you are referring to:
extern alias assembly1;
extern alias assembly2;
using FirstClass = assembly1::Foo.SomeClass;
using SecondClass = assembly2::Foo.SomeClass;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var c1 = new FirstClass();
var c2 = new SecondClass();
}
}
}
Upvotes: 1