Wednesday, April 9, 2014

Visual C# projects properties manual customization - 2/3

Programming language : C#
Topic category : IDE
OS : Windows
IDE : Visual C# 2010 Express

Context

My recent experience with manually editing C# project description files gave me an answer to one of the oldest questions I had about the Visual C# environment: is it possible to have configuration dependent project references?

External files referencing

Coming from the C++ world and being quite experienced with the Visual C++ environment, I'm used to finely configure the static libraries I want to link to projects using the Linker property page of the project properties. I typically use it to link a project with the static library matching the current project configuration (either debug or release).
But there is no such feature in Visual C#. The only way to use code not declared in the solution itself is to reference external files, most often DLLs (from now on, I will refer to such files as "libraries"). Yet much more simple and straightforward, C# library referencing has some drawbacks compared to its C++ counterpart:
  • The list of referenced libraries is identical for all project configurations.
  • Referencing only targets actual files.
Some might argue it doesn't matter. I agree it is clearly not as harmful as in C++, where mixing debug and release code can lead to crashes. I have no idea how different might be debug and release code in .NET, but just a quick look at the Build page of the project properties shows differences, with the default settings:
  • The Optimize code option is checked only for the release configuration.
  • The Define DEBUG constant option is checked only for the debug configuration.


This is why I prefer to ensure the version of the libraries I used match my project configuration, if possible. I was wondering if this could be automated.

Project description file edition

As in my previous article [1], the answer was to come from the project description file customization.
  • I searched for my target library reference description in the project description file (.CSPROJ extension). I found it, described as a Reference element.

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> ... <ItemGroup> <Reference Include="MyLib"> <HintPath>..\..\MyLib\MyLib\bin\Debug\MyLib.dll</HintPath> </Reference> ... </ItemGroup> ... </Project>
  • I updated its path to automatically reflect the current project configuration. This dynamic property can be accessed through the $(Configuration) variable.

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> ... <ItemGroup> <Reference Include="MyLib"> <HintPath>..\..\MyLib\MyLib\bin\$(Configuration)\MyLib.dll</HintPath> </Reference> ... </ItemGroup> ... </Project>
  • Finally, I validated my manual modifications using the environment Object browser. It can be accessed via the main menu > View > Other Windows > Object Browser.



Technical note: I noticed dynamic property dependent values may not be correctly handled by the Properties window. As seen above, the Object browser should be trusted instead.


Conclusion

This basic sample is just a glimpse of what can be achieved through project description file customization. This feature offers much more advanced possibilities.

References

[1] Stef's dev tips 'n' tricks: Visual C# projects properties manual customization - 1/3

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.