0

I am creating a C# class library that will be consumed by another executable outside of my control.

Embedded in my library, I want to use two different versions of the same C++ DLL, which were provided to me.

I want to be able to allow the caller to change the version of the DLL used at run time.

I currently have a project structure that looks like the following:

enter image description here

In my class library, I have some code that looks like this:

if(useOldVersion)
{
    MyOldClr myClr = new MyOldClr();
    return myClr.GetValue();
}
else
{
    MyNewClr myClr = new MyNewClr();
    return myClr.GetValue();
}

I was looking at these two questions to try and solve my problem, but the solution is based around a pure C# context:

Using multiple versions of the same DLL

Using 2 different versions of the same dll?

In my class library, I currently have set up the following in my ClassLibrary.csproj:

  <ItemGroup>
    <Content Include="..\CLRv1\DLL\mydll.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <Link>DLLV1\mydll.dll</Link>
    </Content>
    <Content Include="..\CLRv2\DLL\mydll.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <Link>DLLV2\mydll.dll</Link>
    </Content>
  </ItemGroup>

When I add a reference to the class library in my test project, I can see that both DLLs are being copied to the test executable directory in their appropriate folders.

What I can't figure out is how to get the CLRs to independently reference the correct DLLs. Currently I am getting an "unable to find assembly" error (as expected).

For what it's worth, I am not against changing the project structure. This is simply the approach I have in front of me currently.

JoeBass
  • 519
  • 7
  • 18
  • I know how to do it with unmanaged c++ dll where you cannot add a c++ reference to the .net project. In this case, you have to invoke it using pinvoke where you can load libraries from any path and configure it in runtime which also allows switching – dododo Jun 08 '23 at 18:19

0 Answers0