c# - 为什么.net项目有时会使用旧版本的dll?

标签 c# .net dll

我有项目 Foo1,它依赖于库 Foo2,其中 Foo2 依赖于库 Foo3。

Foo1 是 c# gui 应用程序,Foo2 是 C++/Cli 库,Foo3 是 native c++ 库

Foo2/Foo3 库的 Dll 位于单独的文件夹 Lib 中。 我注意到,有时 Foo1 使用旧版本的 Foo2.dll 文件。它将 dll 从 Lib 文件夹复制到 Bin 文件夹,其中保存可执行文件,并使用它们。当我在 Foo3 库中进行更改时,Foo2 项目也会重建。 Lib 文件夹中的 Dll 已被替换。但它们在 Bin 文件夹中保持未更新,因此主应用程序使用旧的 dll。

它来自 Foo1.csproj 文件:

<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1D3976DD-23E4-4798-80A5-AFA8D34E9342}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AuthorProtoNet</RootNamespace>
<AssemblyName>AuthorProtoNet</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<TargetFrameworkProfile />
<IsWebBootstrapper>false</IsWebBootstrapper>
<!-- Predefined intermediate and output paths, defined for all configurations-->
<IntermediateOutputPath>..\..\Temp\win-$(Platform)-$(Configuration)\$(AssemblyName)\</IntermediateOutputPath>
<OutputPath>..\..\Bin\win-$(Platform)-$(Configuration)\</OutputPath>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<Reference Include="Foo2">
     <HintPath>..\..\..\Lib\win-$(Platform) $(Configuration)\Foo2.dll</HintPath>
</Reference>

最佳答案

MSBuild(Visual Studio 的默认生成引擎)不会添加对非托管库和项目(例如 C++ 库或项目)的引用,因此它们不会复制到项目的输出目录。复制非托管库的常用解决方案是使用构建后事件。构建后事件是 MSBuild 在构建项目后执行的用户配置的 DOS 命令。

您可以在项目的构建后事件编辑器中设置项目的构建后事件;您会在项目的属性 > 构建事件 > 构建后事件命令行中找到它。此外,MSBuild DOS 提示符可以访问所有构建变量(例如 $(Configuration)$(Platform) 等),因此根据构建配置应该不是问题。在您的情况下,类似以下构建后配置(未经测试)的内容应该可以工作。

<PropertyGroup>
  <PostBuildEvent>xcopy \qy ..\..\..\Lib\win-$(Platform) $(Configuration)\Foo2.dll $(OutDir)</PostBuildEvent>
</PropertyGroup>

附注构建后事件编辑器将为您生成前面的部分。不一定需要您自己创建。

关于c# - 为什么.net项目有时会使用旧版本的dll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39878831/

相关文章:

c# - IObservable 按下的键

c# - WPF:使用匿名方法的 UnauthorizedAccessException

c# - Swashbuckle - 未显示继承自 BaseController 的 Web API Controller

c# - 在深度异步调用中查找父方法的属性

c# - 使用 DLL 基地址读取内存

c++ - __cdecl 或 __stdcall 在 Windows 上?

c# - 需要签名 DLL Microsoft.WindowsAPICodePack.Shell

c# - PDFsharp 1.50 测试版 3 : Empty owner password error when adding password to PDF

c# - 从 C# 中的 App.config 文件中读取 key

c# - 将更多项目添加到非常大的 HashSet<Int32> 时出现 OutOfMemoryException