C_Sharp 板


LINE

※ 引述《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)







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:BuyTogether站內搜尋

TOP