Programming language : C#
Topic category : IDE
OS : Windows
IDE : Visual C# 2010 Express
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.
- 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.




No comments:
Post a Comment
Note: Only a member of this blog may post a comment.