我試圖製作一個程序,使用位於(引用).dll中的自定義組件,從內存中運行程序(通過Assembly.Load(bin)
,如here所述)。如何將包含在dll中的組件嵌入到exe中,以便它可以從內存中運行?
由於運行內存代碼運行.exe,組件如何嵌入到.exe中以便不需要.dll?
我試過ILMerge,但生成的.exe不會從內存中運行。 我看了this,但我不認爲它的工作原理,如果你引用的.dll文件(我不得不因爲它包含的窗體上的組件)
更新
讀過NSGaga的答案,我試過如下:
- 設置的組件.dll文件包含在項目中是一個嵌入的資源
作了除了
Program.cs
:using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Windows.Forms; namespace MyApp { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { // System.Diagnostics.Debugger.Break(); String resourceName = "MyApp." + new AssemblyName(args.Name).Name + ".dll"; using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { Byte[] assemblyData = new Byte[stream.Length]; stream.Read(assemblyData, 0, assemblyData.Length); return Assembly.Load(assemblyData); } }; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } } }
不幸的是,這並不工作,我得到一個FileNotFoundException
:
無法加載文件或程序集 'MetroFramework,版本= 1.2.0.0,文化=中性公鑰= 5f91a84759bf584a'或其依賴之一。該系統找不到指定的文件。
Application.Run(new MainForm());
行發生異常。此外,突破/添加消息框等,我不相信AssemblyResolve
處理程序被稱爲 - 我做錯了什麼?
其實我認爲你最後談論的方法是可行的:當CLR搜索一個dll時,它不僅在一個地方查找它,並且'AssemblyResolve'事件也會被觸發,所以你會發現你的DLL。你在項目中引用了dll並不重要。只是嘗試刪除您的調試文件夾的物理DLL,看看會發生什麼 – ppetrov 2013-04-04 22:01:04
我編輯過你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2013-04-05 00:49:40
是不能將嵌入式程序集寫出到存儲設備的要求之一? – 2013-04-06 14:54:04