3

I have a pair of assemblies that implement the same functionality using two different backends:

Engine.OpenGL.dll
Engine.DirectX11.dll

Is it possible to create a single Visual Studio project that is capable of using any of these backends without recompiling? The namespaces/classes/methods exposed by both Engine DLLs are the same (or at least the ones I use in my project are the same).

I tried building my project against one of the Engine assemblies, and then replacing it with the other (by overwriting the dll file). This results in the following error:

The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Do I have to create 2 separate projects with the only difference between them being which Engine DLL they reference?

5
  • 2
    if they implement the same functionality.. perhaps you are running into something know as Method Collision I would suggest either fully qualifying the Method based on the assembly name or Alias the assembly's in your using and qualifying them that way.. also you can't overwrite the Dll or rename once the application has been compiled.. it doesn't work that way.. Commented Jan 15, 2015 at 21:35
  • Have you thought about not having any assembly reference to those two assemblies at all, but loading them dynamically during runtime? msdn.microsoft.com/en-us/library/x4cw969y(v=vs.110).aspx Commented Jan 15, 2015 at 21:39
  • @QualityCatalyst But then I have to use reflection to call anything from Engine - this is not practical in my case. If there was just a few classes/methods then maybe, but there are hundreds. Commented Jan 15, 2015 at 21:40
  • @kaalus If you have shared interfaces defined in a different assembly, you can probably get away with just a bit of reflection to pull out some sort of factory class that's defined in each assembly that can create instances from each assembly as some interface type. Also, these assemblies aren't signed are they? Commented Jan 15, 2015 at 21:47
  • @kaalus how do you ensure your 2 dlls have identical schemas? Commented Jan 15, 2015 at 23:38

1 Answer 1

2

Couldn't you create a project called something like "GraphicsEngineContract" and use that to define interfaces that represent the various classes in your two different engine projects. Then in your engine projects you could make sure that all of your matching classes implement the same interfaces (or abstract classes from the Contract project if any are in there).

You would then refactor the primary project to refer only to the supertypes (abstract classes and interfaces) in the contract project. Finally, your initialization processes would get the ball rolling by detecting what is available on the local systems and assigning concrete instances based on what it finds.

Then it wouldn't be a case of either-or. You would copy both references locally and create objects for them based on what is installed on the end user's machine.

If you followed this approach you would probably end up with four projects. The primary project, contract project, directx project, and opengl project.

Sign up to request clarification or add additional context in comments.

5 Comments

I've done similar to this using autofac - I have one assembly that has interfaces and shared stuff, then the others all reference that assembly. The main program then loads the target assembly and uses DI to register and load the assemblies and interacts with the loaded assembly through the interfaces. Takes a while to set up but after that adding new types is a breeze.
@MrGoodfix This is the proper "object oriented" approach. I think in my case it would be an overkill. Just creating the interface would be a massive undertaking, much bigger than adding a 2nd identical project and living with it. Plus the Engine libraries have a large number of static classes and static methods. All statics would have to be somehow wrapped in interfaces, confusing the api badly.
@kaalus You call this answer overkill but it's the proper solution. Your "problem" screams for this solution. Trying to fool the engine with DLL overwriting would be a nasty hack I would fear in a production environment.
@MattHouser I agree fully this is the proper solution and that overwriting DLLs is a hack. But I have a much simpler solution than an interface. I will just make two projects and have each reference the right library. I thought I can avoid it, but the cost of it is way too large.
I hope you reconsider. You might be able to get the first version out faster, but the cost of maintaining and updating could get pretty steep. A decision like this might save a few hours right now, but it could get really costly later on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.