Conditional Assembly reference

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do you conditionally reference an assembly based off of the build
configuration? Imagine a scenario where I want to have an Sql Release Server
build and an Oracle Release Build and the only difference between them is a
reference to a different assembly.

Is it possible to do this in VS2005?
 
Thanks for your response. So your saying that there isn't a way to do this.
The problem is dynamically loading the assemblies won't help me because the
source wouldn't compile unless one of the two assemblies was referenced.
 
Well, it would help you if you wanted to code the entire thing using
reflection. Which I don't think you do.

I guess you could use reflection to create the objects, then cast them to
the appropriate interface/base class type, and reference them through that
as opposed to the actual class name.

Other then that, you just have to include all the references required for
compile.
 
You're touching on what I know is the right way to do this but is not
possible in my scenario. What really needs to be done is to have
interfaces/abstract classes defined in one assembly and the main application
references only that assembly.
You can then use a plugin architecture to dynamically load the correct
assembly and create the instances behind the scenes.

The problem with my scenario is I want to shove code generated obects
(strong typed datasets) into their own assemblies and there is no way it is
productive to create base interfaces for the code-generated objects.
Especially since they change so much.

This kind of stinks but it just blows this approach out of the water for
dealing with provider generic code.

What I'm really trying to do is come up with a way to get around all the
deficienices of using strong typed datasets and table adapters while trying
to talk with two different databases. It wouldn't have been ideal but it
would have been a start to just have two different kits one for Oracle
support and one for SQL Server.

The old Visual Studios (prior to .NET) also had a way to say that even a
certain file in the project should or should not be built for a particular
build configuration. This might have worked as well.
 
Oldman said:
How do you conditionally reference an assembly based off of the build
configuration? Imagine a scenario where I want to have an Sql Release
Server
build and an Oracle Release Build and the only difference between them is
a
reference to a different assembly.

You can just reference both in VS. Assuming you have #if ORACLE whatever
conditional compilation sections in your code, VS should optimize away the
reference to the unused one in the build. I do this all the time with
NUnit. The NUnit reference is optimized out in release builds and I do not
distribute that DLL with the apps.

-- Alan
 
I figured out my own question.
Although I don't see a way to do it through the IDE you can go into the
csproj file and add an attribute called Condition to the project reference.
For example, you can do the following:

<ProjectReference Include="MyTestProject.csproj" Condition="
'$(Configuration)' == 'Oracle Release' ">
<Project>{ 7E93BE13-47DB-499F-B81F-04B19776352C} </Project>
<Name>MyTestProject</Name>
</ProjectReference>
 
Alan,

That is a great workaround. FYI: I did find a way to actually place a
condition on a reference but your way might be easier. See my other post if
you are interested in how to do it.

-- Chris
 
Back
Top