Remove MSBuild DLL reference regardless of version

2019-05-27 12:50发布

问题:

In a C# project I have a DLL reference that I want to remove before the build (to be replaced with another - this an attempted solution to Easily override NuGet DLL in development (VS 2015)). The following target seems to work:

  <Target Name="BeforeBuild">
    <ItemGroup Condition="Exists('..\..\..\Build\My.dll')">
      <Reference Remove="My, Version=1.2.3.4, Culture=neutral, processorArchitecture=MSIL" />
      <Reference Include="My, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
        <SpecificVersion>False</SpecificVersion>
        <HintPath>..\..\..\Build\My.dll</HintPath>
      </Reference>
    </ItemGroup>
  </Target>

The problem with this is that I need to specify the exact version of the DLL to remove, which can change. I want to remove it regardless of version. I tried a wildcard

      <Reference Remove="My, Version=*, Culture=neutral, processorArchitecture=MSIL" />

... but that didn't seem to match anything, because I got error CS1704: An assembly with the same simple name 'My' has already been imported.

回答1:

Figured it out:

<ItemGroup>
  <ReferenceToBeRemoved Include="@(Reference)" Condition="$([System.String]::Copy(&quot;%(Reference.Filename)&quot;).StartsWith('MyDllName'))" />
  <Reference Remove="@(ReferenceToBeRemoved)" />
</ItemGroup>


回答2:

use:

<Reference Remove="My, Version=1.2.3.4, Culture=neutral, processorArchitecture=MSIL" >
    <SpecificVersion>False</SpecificVersion>
</ Reference>