4

【茶包射手筆記】.NET 專案參照看似一切正常,卻無法編譯

 2 years ago
source link: https://blog.darkthread.net/blog/netfx-version-ref-issue/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【茶包射手筆記】.NET 專案參照看似一切正常,卻無法編譯-黑暗執行緒

因為一個 .NET 參照問題鬼打牆十分鐘,脫身後想起好像不是第一次遇到,肯定是當時沒好好寫篇筆記才被詛咒,趕緊補上。

模擬出相似情境如下,UnitTests 專案參照 MyConsoleApp 專案,呼叫其中 DummyModel.RandomString() 方法(註:範例錯打成 Dymmy 請無視),Visual Studio 有識別出方法帶出註解 [1],而專案 References 裡 MyConsoleApp 圖示也正常未顯示驚嘆號 [2],一切看似正常。

但實際上 UnitTests 無法編譯,出現找不到 MyConsoleApp.dll 錯誤:

以為自己撞鬼了,找了好一陣子,最後在一片 Warning 訊息之間找到線索:

答案出奇簡單,MyConsoleApp 專案是 .NET 4.6.1,UnitTests 是 4.5.2,舊版 .NET Framework 無法參照用新版 .NET Framework 編譯的元件。只是這個問題被歸類成 Warning 而非 Error,而 Visual Studio IDE 能包容這個狀況未給出明顯提示,讓我被迷惑。(後來試了幾次,有時 References 項目會出現如下圖的黃色警嘆號,有時不會)

有此經驗,下回再遇到參照看似正確卻編譯失敗的狀況,SOP 加入先拆座墊先檢查 Warning 排除 .NET Framework 版本相容問題準沒錯。

and has 0 comments

Comments

Be the first to post a comment

Post a comment

Comment
Name Captcha 43 - 4 =

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK