I have a project which needs to reference two DLLs with the same name. The DLLs are not strong named, have the same exact name.
I need to access some types within each DLL, but these types have the same fully qualified name.
So let's say the first one is companyDLL.dll with someProduct.Type1
and
the second one is companyDLL.dll with someProduct.Type1
.
How can I access both Type1
classes within the same project?
I have already tried using extern alias
, but it requires me to change the name of one of the DLLs.
Yes, you can split the namespace into multiple blocks (and hence files). Your classes will belong to the same namespace as long as they are declared in the namespace block with the same name.
config. Under <runtime> , add a <codeBase> tag for each version of the DLL. This will resolve the runtime assembly loading conflict. That's it, now we can use both versions as we please.
You can certainly embed another DLL as a resource and extract and load it at runtime if that's what you need.
DLLs are created with a default base address. Every process that uses a DLL will try to load the DLL within its own address space at the default virtual address for the DLL. If multiple applications can load a DLL at its default virtual address, they can share the same physical pages for the DLL.
If your two DLLs have the same name, you are going to have to rename them. Such as Assembly1.dll and Assembly2.dll.
Add these DLLs as a reference in your project as you normally would and in the properties for each reference specify an alias.
in your code when using the DLLs use extern alias
to specify what dll you want to reference.
extern alias Assembly1Reference;
using Assembly1Reference::AssemblyNamespace.MyClass;
If you leave it like this, you will most likely get a FileNotFoundException
saying that it Could not load file or assembly. To fix this you need to add a ResolveEventHandler
that will load the correct assembly you are trying to use. To do this you have to specify exactly where you are storing your DLL files. In the case below, I manually copied the Dll files to the projects debug folder. Where it says "name of assembly1" you can find the name after you reference the DLL, build the project, and open the csproj file with notepad. What to look for will be below my example code.
extern alias Assembly1Reference;
extern alias Assembly2Reference;
static void Load()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
Do();
}
static void Do()
{
new Assembly1Reference.Assembly.Class();
new Assembly2Reference.Assembly.Class();
}
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string currentPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
if(args.Name == "Name of assembly1")//Found in csproj file after referenced and built
{
return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(currentPath, "Assembly1.dll"));
}
if(args.Name == "Name of assembly2")//Found in csproj file after referenced and built
{
return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(currentPath, "Assembly2.dll"));
}
return null;
}
As promised, here is what a reference looks like in the csproj file. The name is everything inside the include attribute.
<Reference Include="MyAssembly_3.6.2.0, Version=3.6.2.0, Culture=neutral, PublicKeyToken=12341234asdafs43, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>Resources\Assembly1.dll</HintPath>
<Aliases>Assembly1Reference</Aliases>
</Reference>
I know this is late but hopefully it will help anyone coming to this page from now on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With