[C#]使用Costura.Fody將源DLL合并到目標EXE

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.