views:

329

answers:

2

Hi, I want to load one of four separate C# assemblies based upon what the build target is. This is going into a webservice with .net framework 3.0.

possibilities:

32-bit debug: AmtApiWrapper32d.dll

32-bit release: AmtApiWrapper32.dll

64-bit debug: AmtApiWrapper64d.dll

64-bit release: AmtApiWrapper64.dll

Those wrappers are a separate C++ project that wrap a C Native DLL that I wrote. C/C++ is my usual platform so please excuse me if this is a nubs question.

All of the wrapper DLLs contain the exact same functions and same prototypes. They're used for many other purposes besides this one, so unless this is really terrible, the setup stays the same.

So, i want to load one of them at compile time. I've looked over stuff like reflection, GetDelegateForFunctionPointer, and some other stuff, and they all seem similar but overly complicated for this simple task. Any suggestions? THANKS

A: 

Have you tried using pre-compiler directives and defining different variables in the build target?

i.e.

#ifdef 32bdebug
<load dll here>
#endif

It's been a little while since I've used C#, but this should work at compile time. It's one of C#'s takes from C and C++.

Chris J
Thanks, in C/C++ you'd use LoadLibrary() within the ifdef, but i need the C# version. the suggestion from Jon Skeet seems like the cleanest way. thanks
rajat
+6  A: 

It's definitely possible, but you'll have to wade into the build file.

You'd want something like:

<ItemGroup Condition=" '$(Configuration)' == '32-bit debug' ">
  <Reference Include="AmtApiWrapper32d">
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)' == '32-bit release' ">
  <Reference Include="AmtApiWrapper32">
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)' == '64-bit debug' ">
  <Reference Include="AmtApiWrapper64d">
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)' == '64-bit release' ">
  <Reference Include="AmtApiWrapper64">
</ItemGroup>

I suggest you unconditionally get one of them working, and then look at exactly what the reference looks like.

That's if you want to add it as a build-time reference. If you want to use it in a P/Invoke declaration, just use #if SYMBOL/#endif around the appropriate attribute.

Jon Skeet
CLOSE! Thanks Jon. Here:<ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <Reference Include="IcsAmt.Wrapper32d.dll"></Reference> </ItemGroup>Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU'Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64'etc
rajat