Merec
Merec

Reputation: 2761

Load different assembly depending on build configuration

I am using Magick.NET in my project, which renders maps of a game. Is it possible to load the X86 version when I build the tool for x86 processors and the X64 version of Magick.NET for 64bit processors? I guess the the main problem is, that the different versions of the dll have different identities.

I have written a prebuild event which copes the right dll on build, depending on the selected build configuration. I also loaded the copied assembly using Assembly.LoadFrom();, but the app always throws an exception that the assembly I added to references in VS2010 was not found. I can not add both references because they have the same namespace and the same methods.

Is there a better way to handle this except changing the reference in VS2010 for each build configuration?

Prebuild process:

echo."$(ConfigurationName)"|findstr /C:"x64" >nul 2>&1
if not errorlevel 1 (
   copy /Y "$(ProjectDir)lib\Magick.NET-x64.dll" "$(TargetDir)lib"
) else (
   copy /Y "$(ProjectDir)lib\Magick.NET-x86.dll" "$(TargetDir)lib"
)

Load assembly on x86 build:

if (IntPtr.Size == 4)
{
    Assembly assembly = Assembly.LoadFrom("lib/Magick.NET-x86.dll");
}

Exception:

System.BadImageFormatException
Magick.NET-x64, Version=6.0.0.0, Culture=neutral, PublicKeyToken=2004825badfa91ec not found

Upvotes: 1

Views: 1244

Answers (1)

PMF
PMF

Reputation: 17328

I made the experience that it's best to fully separate x86 and x64 builds. So create a build configuration for each platform/build combination (usually, there will be at last four: x86 debug, x86 release, x64 debug, x64 release), with a separate output directory for each configuration for every project in the solution. It is a bit of effort in the beginning because some manual tweaking of the project files might be necessary (config dependent reference hint paths), but in the end it causes the least problems.

Upvotes: 1

Related Questions