Search code examples
c#msbuild.net-6.0csproj

Using variable in library dependency path in .NET


I'm developing a .NET 6 C# application, and I have some projects in the solution that depend on an external DLL library.

What I would like is being able to use a variable in the path to the library reference in the .csproj file. I thought you could do something like this:

<PropertyGroup>
    <LibVer>v1.2</LibVer>
</PropertyGroup>

<ItemGroup>
    <Reference Include="MyLibrary">
        <HintPath>..\libs\${LibVer}\MyLibrary.dll</HintPath>
    </Reference>
</ItemGroup>

However, this doesn't work, as soon as I do it the project doesn't compile anymore. I don't get any specific error in the compilation output, all I get is that any type from the included library is not recognized, with the classic error:

error CS0246: The type or namespace name 'XXXXX' could not be found (are you missing a using directive or an assembly reference?)

Can this be done? If so, what is the correct way?

(some context as to motivation: I have several different versions of this dependency and I would like to be able to change just one variable in a common .csproj file and have all the solution's projects that include the dependency load the correct one)


Solution

  • Well, it turns out it was just a dumb typo on my end, I was using braces instead of brackets.

    This works correctly:

    <HintPath>..\libs\$(LibVer)\MyLibrary.dll</HintPath>