標籤:set 網盤 stat using 沒有 4.6 開啟 pre under
一、本文主要是使用Costura.Fody工具將源DLL合并到目標EXE,因此,需要從以下任一連結下載:
①從Github地址下載:
https://github.com/Fody/Costura/releases
②從百度網盤下載:
https://pan.baidu.com/s/1kV9W34b
③【推薦】從Nuget地址安裝工具:
https://www.nuget.org/packages/Costura.Fody/
並從Visual Studio中的封裝管理員控制台進行安裝:
PM> Install-Package Costura.Fody -Version 1.6.2
註:最新版本請開啟Nuget地址進行擷取
二、安裝之後,Costura.dll等已經被引用進來,如所示:
三、建立一個引用Newtonsoft.Json.dll的解決方案,這個就借用上一篇內容【[C#]使用ILMerge將源DLL合并到目標EXE(.NET4.6.2)】的例子,解決方案:
https://pan.baidu.com/s/1jIzjpkU
代碼如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Newtonsoft.Json;using Newtonsoft.Json.Linq;namespace ConsoleApp12{ class Program { static void Main(string[] args) { List<Person> list = new List<Person>() { new Person(){ ID = 1, Name = "ABC" }, new Person(){ ID = 2, Name = "XYZ" }, }; var result = JsonConvert.SerializeObject(list); JArray jArray = JArray.Parse(result); foreach(var item in jArray) { Console.WriteLine($"ID:{(int)item["ID"]},Name:{(string)item["Name"]}"); } Console.ReadKey(); } } class Person { public int ID { set; get; } public string Name { set; get; } }}
引用的Newtonsoft.Json.dll如上一張圖片所示。
四、點擊運行按鈕,然後在bin/Debug檔案夾下看能產生的檔案如下:
從以片可以看出,產生的檔案沒有包含Newtonsoft.Json.dll與Costura.dll沒有被產生,只有三個檔案。
我們可以刪除ConsoleApp.exe.config及ConsoleApp12.pdb檔案,留下ConsoleApp12.exe檔案即可,ConsoleApp12.exe能單獨運行。
五、我們可以使用ILSpy.exe查看剛才產生之後的ConsoleApp12.exe,如所示:
從以上可以看出,可以使用Costura.Fody將源DLL合并到目標EXE。
[C#]使用Costura.Fody將源DLL合并到目標EXE