問題描述
寫.建議的欺騙:因為這里的queston建議與鏈接問題,我寧愿認為它不是是騙子.
Wrt. the proposed dupe: Since this here queston suggests the opposite of the linked question, I'd rather like to think it is not a dupe.
首先,我確實閱讀了 復制本地"和項目引用的最佳做法是什么?(也 this) 無論如何我都必須嘗試一下,但是獲得對此的一般反饋似乎是必要的,因為 <關于這些東西的 rel="noreferrer">docs 太可怕了我只在 VS2010 上,也許他們在較新的版本中改變了一些東西,很高興知道.
First, I did read What is the best practice for "Copy Local" and with project references? (also this) and I'll have to try this out anyway, but getting general feedback on this seems necessary as the docs on this stuff are horrible and I'm only on VS2010 and maybe they changed something in newer versions that'll be nice to know.
第二,我只對這個問題的項目參考感興趣,因為我已經 閱讀來自 GAC 的程序集的處理方式不同,而 GAC 與我的問題無關.
Second, I'm only interested in project references for this question as I've read that assemblies from the GAC are handled differently and the GAC is irrelevant for my problem.
第三,在閱讀了建議的欺騙之后,但更重要的是這里的 answer@Albireo,區分 file 依賴項似乎也很重要,其中依賴項引用 dll 程序集文件和 project 依賴項(即我要問的),其中依賴項引用了一個項目,并且隱式地引用了該項目的輸出文件.
Third, after reading the suggested dupe, but more so the nice answer here by @Albireo, it would also appear that it is important to differentiate file dependencies, where the dependency references a dll assembly file and project dependencies (i.e. what I'm asking about), where the dependency references a project and implicitly the output file of that project.
- 2 個 C# 可執行項目
- n C# dll 程序集項目
- 這 2 個可執行文件具有不同的輸出目錄,因為它們將單獨部署,因此它們在開發人員計算機上也是獨立的
- 這 2 個可執行文件依賴于某些 DLL 程序集(它們可能相互依賴)
- 共有三個輸出目錄:
/x1
用于可執行 1 項目/x2
用于可執行 2 項目/lib
用于所有 dll 程序集
- 2 C# executable projects
- n C# dll assembly projects
- The 2 executables have different output directories as they will be deployed separately and that way they're also separate on the developer machine
- The 2 executables have dependencies on some of the DLL assemblies (which may depend on each other)
- There are three output directories:
/x1
for executable 1 project/x2
for executable 2 project/lib
for all the dll assemblies
DLL 程序集all 將
Copy Local
設置為false
以供其項目引用,因為它們都構建到相同的輸出目錄.The DLL assemblies all have
Copy Local
set tofalse
for their project references, as they all build to the same output directory.這 2 個可執行項目已將它們引用的所有 DLL 程序集項目引用的
Copy Local
設置為true
直接將DLL復制到/x1
/x2
中.The 2 executable projects have set
Copy Local
totrue
for all the DLL assembly project references they reference directly, so that the DLLs will be copied into/x1
/x2
respectively.問題現在是 wrt.到可執行項目不直接引用但僅通過引用的程序集傳遞的DLL:Will程序集,僅當復制本地"在第一個程序集上設置為 true 時,通過另一個程序集傳遞引用,復制到可執行文件的輸出文件夾?
The question now is wrt. to DLLs that are not directly referenced by an executable project, but only transitively through a referenced assembly: Will assemblies, that are only referenced transitively through another assembly, be copied into the output folder of the executable, when "Copy Local" is set to true on the first assembly?
示例:
x1.csproj
(例如輸出 =x1/one.exe
)- 參考:
dlA.csproj
(例如 Output =lib/a.dll
)與Copy Local = *true*
- (沒有直接引用 b.dll)
x1.csproj
(e.g.Output =x1/one.exe
)- Reference:
dlA.csproj
( e.g. Output =lib/a.dll
) withCopy Local = *true*
- (no direct reference on b.dll)
- 參考:
dlB.csproj
(例如 Output =lib/b.dll
)與Copy Local = **false**
李> - (沒有直接引用 c.dll)
- Reference:
dlB.csproj
( e.g. Output =lib/b.dll
) withCopy Local = **false**
- (no direct reference on c.dll)
- (沒有進一步的相關參考資料)
因此,我們有
one.exe -> 的邏輯依賴.a.dll ->b.dll ->c.dll
,其中只有a.dll
明顯被復制到one.exe
的輸出目錄.其他兩個 dll 是否也會被復制到輸出目錄中? 這是否記錄在某個地方?Thus, we have a logical dependency of
one.exe -> a.dll -> b.dll -> c.dll
, where onlya.dll
with obviously be copied to the output directory ofone.exe
. Will the other two dlls also be copied to the output directory? Is this documented somewhere?而且,是的,我試過了.而且,是的,它似乎可以工作,但我還沒有足夠努力地戳它,無論如何,我可能錯過了更多的東西.(還有任何官方文檔的問題.)
And, yes, I tried it. And, yes, it seems to work, but I haven't poked it hard enough yet and anyway there maybe something more to it that I may have missed. (And also there's the question wrt. any official docs.)
推薦答案
似乎區分文件依賴項也很重要,其中依賴項引用 dll 程序集文件和項目依賴項(即我要問的),其中依賴項引用項目并隱含地輸出該項目的文件項目.
it would also appear that it is important to differentiate file dependencies, where the dependency references a dll assembly file and project dependencies (i.e. what I'm asking about), where the dependency references a project and implicitly the output file of that project.
不是真的,不.
MSBuild 并不真正關心引用是指向解決方案中的另一個項目還是指向 DLL.
MSBuild doesn't really care if the reference points to another project in the solution or to a DLL.
如果
ProjectA
依賴ProjectB
來構建ProjectA
ProjectB
必須已經構建(并且最多日期),然后 MSBuild 將提取其 DLL(而不是其 C# 代碼)并將其鏈接到ProjectA
.If
ProjectA
depends onProjectB
to buildProjectA
ProjectB
must be already built (and up-to-date), MSBuild will then pull its DLL (not its C# code) and link it toProjectA
.添加項目引用而不是 DLL 是語法糖".為了您的方便:這樣 MSBuild 知道它必須選擇引用項目的輸出,無論輸出是什么.
Adding a project reference instead of a DLL is "syntactic sugar" for your convenience: this way MSBuild knows it must pick the output of the referenced project, whatever the output is.
否則,您將不得不手動預構建依賴項,找到它的 DLL 并將其鏈接到項目,每當您切換構建配置、移動或重命名時都重復該過程.不太實用.
Otherwise, you'll have to manually pre-build the dependency, find its DLL and link it to the project, repeating the process whenever you switch build configuration, move or rename things. Not really practical.
另外兩個dll也會復制到輸出目錄嗎?
Will the other two dlls also be copied to the output directory?
如果直接從引用程序集的項目中使用依賴項中的任何類型的元素,則將復制該引用.
If any kind of element from a dependency is used directly from the project where the assembly is referenced, that reference will be copied.
一個例子可能是這個解決方案布局:
An example could be this solution layout:
- 我的解決方案
- MySolution.ConsoleApplication
- MySolution.FirstDependency
- MySolution.SecondDependency
- MySolution.ThirdDependency
- MySolution.FourthDependency
有了這個依賴鏈:
- MySolution.ConsoleApplication
- MySolution.FirstDependency
- MySolution.SecondDependency
- MySolution.ThirdDependency
- MySolution.FourthDependency
如果您構建此解決方案,您會注意到在
MySolution.ConsoleApplication
輸出目錄中將有MySolution.FirstDependency
、MySolution.SecondDependency<的 DLL/code> 和
MySolution.ThirdDependency
但沒有MySolution.FourthDependency
的 DLL.If you build this solution you'll notice that in
MySolution.ConsoleApplication
output directory there will be the DLLs forMySolution.FirstDependency
,MySolution.SecondDependency
andMySolution.ThirdDependency
but no DLL forMySolution.FourthDependency
.為什么會這樣?當 MSBuild 構建
MySolution.SecondDependency
時,它注意到向MySolution.FourthDependency
聲明了一個依賴項,但由于它無法從MySolution.FourthDependency
在MySolution.SecondDependency
代碼中它決定執行一些優化";并從輸出中省略MySolution.FourthDependency
程序集.Why is it so? When MSBuild builds
MySolution.SecondDependency
it notices that there's a dependency declared toMySolution.FourthDependency
, but since it can't find any usage of any kind of element fromMySolution.FourthDependency
inMySolution.SecondDependency
code it decides to perform some "optimization" and omitsMySolution.FourthDependency
assembly from the output.過去,當我通過 NuGet AutoMapper 添加到深度依賴"時,同樣的問題困擾著我:添加 AutoMapper 會添加兩個程序集引用,
AutoMapper
和AutoMapper.Net4
,當第二個程序集需要對 .NET Framework 4 引入的新集合對象執行某種操作時,第二個程序集由第一個程序集通過反射加載.由于第二個程序集是通過反射加載的,MSBuild 認為它未使用并且沒有懶得到處抄.This same issue bit me in the past when I added through NuGet AutoMapper to a "deep dependency": adding AutoMapper adds two assembly references,
AutoMapper
andAutoMapper.Net4
, where the second assembly is loaded by the first through reflection when it needs to perform certain kind of action on the new collection objects introduced by the .NET Framework 4. Since the second assembly is loaded through reflection MSBuild thinks it's unused and doesn't bother to copy it around.所以,是的,只要您直接使用它們,它們就會被復制,而不是通過反射.
So, yes, they will be copied as long as you're using them directly and not through reflection.
這是否記錄在某處?
這種行為似乎是一種特征".在 MSBuild 中,當我遇到此問題時,我設法找到了 Microsoft 一些人的博客文章,但目前我無法再次找到它.
This behavior seems to be a "feature" of MSBuild, I managed to find a blog post by some folks from Microsoft back when I experienced this issue, but I can't find it again at the moment.
這篇關于是“復制本地"項目引用的傳遞?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
- MySolution.SecondDependency
- Reference:
- 參考: