Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET resolves a class to the wrong DLL

My C# project has references to two third party DLLs. This is important because it means I don't have access to source code and can't modify or recompile these two DLLs.

Let's call them dll A and dll B. Here's what dll A looks like:

namespace ThirdParty.Foo
{
  public class Bar
  {
    ...snip...
  }

  public class Something
  {
    public Bar MyProperty { get; set; }
  }
}

And here's what dll B looks like:

namespace ThirdParty.Foo
{
  public class Bar
  {
    ...snip...
  }

  public class SomethingElse
  {
    public Bar MyProperty { get; set; }
  }
}

As you can see, they have the same namespace and they both define a class with the same name. My C# code needs a reference to both DLLs. I use the alias property on the reference to be able to distinguish between the two references and I also extern alias firstDll and extern alias secondDll at the top of my C# file. So far so good.

It seems obvious to me that the type of Something.MyProperty is firstDll.ThirdParty.Foo.Bar and the type of SomethingElse.MyProperty is secondDll.ThirdParty.Foo.Bar but for some reason, Visual Studio gets confused and resolves the type of both properties to the same Bar class in firstDll.

Is there a way for me to "force" VisualStudio to resolve the correct type?

EDIT: the error I'm getting in Visual Studio is: Cannot implicitly convert type 'ThirdParty.Foo.Bar [d:\MySolution\References\Second.dll]' to 'ThirpParty.Foo.Bar [d:\MySolution\References\First.dll]'

like image 781
desautelsj Avatar asked Aug 06 '13 23:08

desautelsj


3 Answers

Create a DLL that will serve as a wrapper for DLL A. Let's call the new DLL "C". Your project will then reference DLL B and C.

like image 163
Tarik Avatar answered Nov 15 '22 07:11

Tarik


If the two types have the same name and namespace, then you are pretty much stuck. In C#, the name/identifier is quite important, "best fit" will only work in special situations.

However, you could write wrappers for the two different types, and make those wrappers have different names. By having two different (or even one) project just for the wrapper, you could have only one reference, thus effectively resolving the conflict "by force".

like image 2
Jan Dörrenhaus Avatar answered Nov 15 '22 07:11

Jan Dörrenhaus


Something that works(for me it did referenced correctly) is the following,first(as you probably already did)click each dll in the references and assign an alias and place the proper extern alias.After to use the class Something and SomethingElse(and properly assign the Bar Property) create one class for each of those classes(Something and SomethingElse)and derive from them and shadow the MyProperty property:

public class TestFirst : first.ThirdParty.Foo.Something
{
    //here you shadow and since you must provide the alias
    //and the fully qualified name it will bet set to the
    //right Bar class,same bellow in testsecond.
    public first.ThirdParty.Foo.Bar MyProperty { get; set; }
}

public class TestSecond : second.ThirdParty.Foo.SomethingElse
{
    public second.ThirdParty.Foo.Bar MyProperty { get; set; }
}

After its just business as usual:

TestSecond t = new TestSecond();
t.MyProperty = new second.ThirdParty.Foo.Bar();
like image 2
terrybozzio Avatar answered Nov 15 '22 09:11

terrybozzio