1

the scenario C# VS2013 I have a solution with 2 projects A and B with project A referencing the classes in project B.

Project B contains relatively static classes which are environment specific to allow interfacing in a particular environment in this case production.

I want to create a clone of Project B with with development environment based classes and have it used instead of the production version when built using a dev configuration.

I don't want to have to alter the Project A codebase in any way.

How can I achieve this?

m12lrpv
  • 997
  • 2
  • 11
  • 18
  • Use #IF. See msdn : https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if – jdweng Oct 24 '17 at 01:43
  • Possible duplicate of [Visual Studio Project: How to include a reference for one configuration only?](https://stackoverflow.com/questions/1493749/visual-studio-project-how-to-include-a-reference-for-one-configuration-only) – Samir Aguiar Oct 24 '17 at 02:05
  • Or maybe https://stackoverflow.com/questions/14335186/changing-implementation-of-interface-depending-on-environment which is more inline with having multiple implementations... I'd strongly advice against creating "clone of Project B" - copying code usually leads to maintenance problems. Most likely picking some configuration framework with or without IoC likely would be better choice. – Alexei Levenkov Oct 24 '17 at 03:30
  • @Samir Aguiar solution may have some similarities but the question is not a duplicate. I'm trying to swap an otherwise identical dll which has the potential to have naming conflicts. The issue you linked to is too simplistic and besides if it was a duplicate then the search would have found it. – m12lrpv Oct 24 '17 at 04:24

1 Answers1

0

That is relatively easy to achieve. It requires hand editing of some of your project files. First create clone of Project B, than open new csproj file in an editor and change project GUID to a new one (do not confuse it with project type Guid) and to avoid future mistakes change new project file name as well.

In Project A the only changes which are required are related to Project B csproj references where we want to make them conditional on some environment / build settings and depending on them choose production or developer Project B reference.

Below I have created copy of project references section of one of CppSharp projects. The project reference to CppSharp project was splitted into two alternatively included references to CppSharp.csproj when DevelopmentBuild environment or MSBuild property is false and reference to CppSharp.Dev.csproj which is included when DevelopmentBuild is set to True.

  <ItemGroup>
    <ProjectReference Include="..\Core\CppSharp.csproj" Condition"$(DevelopmentBuild) != 'True'">
      <Project>{C600C309-B2CD-1D15-DBE6-0BBDC71253A3}</Project>
      <Name>CppSharp</Name>
    </ProjectReference>
    <ProjectReference Include="..\CoreDev\CppSharp.Dev.csproj" Condition"$(DevelopmentBuild) == 'True'">
      <Project>{C600C309-B2CD-1D15-DBE6-0BBDC71253A4}</Project>
      <Name>CppSharp</Name>
    </ProjectReference>
    <ProjectReference Include="..\AST\CppSharp.AST.csproj">
      <Project>{BC4C5C41-A8AF-EBE5-5135-249C3D77B768}</Project>
      <Name>CppSharp.AST</Name>
    </ProjectReference>
    <ProjectReference Include="..\Runtime\CppSharp.Runtime.csproj">
      <Project>{189FF169-0498-10BC-2DCA-F5401922F0C7}</Project>
      <Name>CppSharp.Runtime</Name>
    </ProjectReference>
    <ProjectReference Include="..\..\build\vs2015\projects\CppSharp.Parser.CLI.vcxproj">
      <Project>{C75EB680-33C0-938E-BC2D-DA40288ECA4D}</Project>
      <Name>CppSharp.Parser.CLI</Name>
    </ProjectReference>
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Jacek Blaszczynski
  • 3,183
  • 14
  • 25