Re: [問題] 如何避免引用元件?

看板C_Sharp (C#)作者 (短長肥脊各有態)時間20年前 (2004/10/27 22:39), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《tomex (tomex_ou)》之銘言: : 在vs.net 2003裏頭, : 一個a專案,若引用(refer)了某外部元件b : compile成功後會在/bin/有一個a.dll及b.dll : 若新增一個c專案,refer了a.dll,那個在c專案的/bin裏 : 會自動複製了a.dll及b.dll。 : 然而c專案裏某個d.class需用到b.dll的namespace : 那麼如何避免c專案去引用b.dll呢? : (因為b.dll在引用a.dll就存在了,若硬再refer一次,會破壞設定) : 大家聽得懂嗎? : 就是某段程式要用到b.dll裏頭的物件type,但專案若不引用b.dll就看不到! : 然而明明在引用a.dll時vs.net2003就會把b.dll複製到/bin下了呀, : 再重複挑一次很顯然多餘的! 不挑,vs.net2003就是會看不到該namespace! : 我曾想過在a.dll寫一些空的類別,來繼承b.dll裏頭的class : 但是b.dll若有上千上萬的class,就有點煩了! : namespace又不能繼承?! : 我最終目的,就是要讓引用的動作全部在a專案就設定完成 : 其他專案只需引用a.dll專案即會自動copy相關的reference dll : 避免單獨挑選... 抄自.Net Framework SDK 文件.NET Framework 工具 強式名稱工具 (Sn.exe) 幫助以強式名稱 (Strong Name) 建立組件。Sn.exe 提供了金鑰管理、 簽章產生和簽章驗證的選項。 全域組件快取工具 (Gacutil.exe) 可讓您檢視和管理全域組件快取的內容並下載快取。雖然 Shfusion.dll 提供了 類似功能,不過您可以從建置的指令碼、Makefile 檔案和批次檔使用 Gacutil.exe。 STEP 1: To generate a key pair 1. Open the Visual Studio .NET Command Prompt. In Windows XP, this can be opened from Start, All Programs, Microsoft Visual Studio .NET, Visual Studio .NET Tools. 2. At the prompt, use the -k flag with sn.exe to specify an output file for your key pair. Key pair files usually have a .snk extension. An example follows: sn –k myKey.snk STEP 2:To sign your assemblies with a strong name In Solution Explorer, open the AssemblyInfo file for your project. [assembly: AssemblyVersion("1.0.1.1")] //版本隨便 [assembly: AssemblyKeyFile("..\\..\\myKey.snk")] STEP 3: To install your assembly to the Global Assembly Cache 1. Sign your assembly with a strong name. 2. Open the Visual Studio .NET Command Prompt. In Windows XP, this can be opened from Start, All Programs, Microsoft Visual Studio .NET, Visual Studio .NET Tools. 3. Run gacutil.exe with the /i option to specify the assembly to be installed. The following code example demonstrates how to install an assembly named myAssembly.dll to the Global Assembly Cache: gacutil /i mypath\myAssembly.dll ~~~~~~~~~ 好像要寫完整path, 忘了 STEP 4: Where can I find my global assembly? In XP C:\WINDOWS\assembly\ (選:詳細資料) 資料夾內有全域組件名稱,類型,版本,文化特性,公開金鑰語彙基元 STEP 5: 其他專案也可以引用,In Solution Explorer 加入參考 ------------------------------------------------------------------------------ Step by Step Example 前提: 1. 本例子使用Visual Studio .Net 2003 2. 本例子DOS指令皆在Visual Studio .NET 2003 命令提示字元中執行 (開始→所有程式→Microsoft Visual Studio .NET 2003→Visual Studio .NET工具) 3.路徑請自行調整 ------------------------------------------------------------------------------ 1. C:\>sn -k myKey.snk 2. 開啟新的類別庫專案ClassLibrary1 //類似版大的b.dll (1) In Class1.cs using System; namespace ClassLibrary1 { /// <summary> /// Class1 的摘要描述。 /// </summary> public class Class1 { private int temp; public Class1() { temp = 10; } public int Temp { get { return temp; } } } } (2)In AssemblyInfo.cs 請修改成下列此行 [assembly: AssemblyKeyFile("C:\\myKey.snk")] (3)請把Debug改成Release模式,然後建置此專案 3. 開啟新的類別庫專案ClassLibrary2 // 類似版大的a.dll (1)在方案總管中將Step2中剛建置的DLL加入參考 (2)In Class1.cs using System; namespace ClassLibrary2 { /// <summary> /// Class1 的摘要描述。 /// </summary> public class Class2 { private ClassLibrary1.Class1 c; public Class2() { c = new ClassLibrary1.Class1(); } public int SquareClass1() { return c.Temp * c.Temp; } } } (2)In AssemblyInfo.cs 請修改成下列此行 [assembly: AssemblyKeyFile("C:\\myKey.snk")] (3)請把Debug改成Release模式,然後建置此專案 (4)此時\bin\Release\中有ClassLibrary1.dll 及ClassLibrary2.dll 4. C:\...\ClassLibrary2\bin\Release>gacutil /i ClassLibrary1.dll 5. C:\...\ClassLibrary2\bin\Release>gacutil /i ClassLibrary2.dll 6. 開啟新的Windows應用程式專案WindowsApplication1 // 類似版大的c專案 (1)在方案總管中加入參考→剛剛建置的ClassLibrary2.dll (2)In Form1.cs 拉一個Label跟Button到Form1上 (3)在button1上按兩下 private void button1_Click(object sender, System.EventArgs e) { ClassLibrary2.Class2 c = new ClassLibrary2.Class2(); label1.Text = c.SquareClass1().ToString(); } (4)啟動此專案 (5)按button,label從label1變成100 (6)在\bin\Release\中只有WindowsApplication1.exe 7.在C:\WINDOWS\assembly中可看到(請用檔案總管不要用命令提示字元看) 全域組件名稱 類型 版本 文化特性 公開金鑰語彙 ClassLibrary1 1.0.1762.38750 8417f199c4a717ce ClassLibrary2 1.0.1762.38808 8417f199c4a717ce 8.寫了這麼多,但這是你要的嗎? ※ 編輯: chiifan 來自: 219.84.81.23 (10/28 23:14)
文章代碼(AID): #11VxCiR4 (C_Sharp)
討論串 (同標題文章)
文章代碼(AID): #11VxCiR4 (C_Sharp)