用于批量替换 C# 解决方案中的 DLL 引用(At VisualStudio)。这是一篇使用指南。 介绍可以看这里
目的
进行源码调试。 通常,我们使用DLL引用(或 Nuget 引用),但这样很不方便调试,尤其对于内部项目之间,明明两边的代码都有,为啥不能联合调试呢。
原理
将所有引用目标DLL的工程文件(.csproj文件)中的引用替换为 ProjectReference
。 引用目标DLL对于的工程文件(.csproj)。
使用步骤
入口
- 点击 “SELECT .CSPROJ FILE” 按钮。
- 选择目标工程的 csproj 文件。
- 点击 “REPLACE REFERENCE” 按钮完成替换。
这时 VS 会提示你重新加载解决方案,重新加载之,之后此解决方案中所有的引用就是新指定的引用了。
撤销是怎么做的?
因为替换的操作,其实就是对 csproj 文件的操作,撤销,就是把 csproj 恢复原样咯。
这里使用的是 git 命令 checkout。
所以,在替换之前,会检测是否有 csproj 文件和 sln 文件的修改,如果有,会提示不能替换,除非取消勾选下方的 Use git checkout command when Undo
。
- 取消勾选了
Use git checkout command when Undo
会怎么样?
1、将不能使用撤销按钮进行撤销操作。
要撤销怎么办? 可以手动敲命令 git checkout *.csproj
和 git checkout *.sln
注意,自己不能有对 csproj 文件的修改哦。
2、可以替换多个DLL引用。
取消勾选之后,不会对 csproj 文件的修改进行检查,所以可以任意替换。
- 替换之后,关闭了 VS ,怎么撤销?
参考上面第1条。
手动操作
如果不使用工具,如何手动操作。拢共分两步,
1 在 csproj 中取出之前的引用,替换成源码引用
如下方的例子,将 PackageReference 替换成 ProjectReference
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <TargetFramework>net8.0-windows</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <Platforms>AnyCPU;x64</Platforms> </PropertyGroup>
<ItemGroup> <!--<PackageReference Include="fo-dicom" Version="5.1.3" />--> </ItemGroup>
<ItemGroup> <ProjectReference Include="..\..\..\..\github\fo-dicom\FO-DICOM.Core\FO-DICOM.Core.csproj" /> </ItemGroup>
</Project>
2 使用 dotnet 工具,将引入的 project 添加到解决方案中
dotnet sln command - .NET CLI | Microsoft Learn
dotnet sln "xxxx.sln(sln绝对路径)" add "xxxx.csproj(csproj绝对路径)"
相关知识
- How to get relative path from absolute path
- .net reference specificversion true or false?
- HintPath vs ReferencePath in Visual Studio
There is a search order for assemblies when building. The search order is as follows:
- Files from the current project – indicated by ${CandidateAssemblyFiles}.
- $(ReferencePath) property that comes from .user/targets file.
- %(HintPath) metadata indicated by reference item.
- Target framework directory.
- Directories found in registry that uses AssemblyFoldersEx Registration.
- Registered assembly folders, indicated by ${AssemblyFolders}.
- $(OutputPath) or $(OutDir)
- GAC
相关链接
原文链接: https://blog.jgrass.cc/posts/auto-dll-reference-change-tool/
本作品采用 「署名 4.0 国际」 许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。