C#程序集系列06,程序集清单,EXE和DLL的区别
2023-09-11 14:16:51 时间
CLR在加载程序集的时候会查看程序集清单,程序集清单包含哪些内容呢?可执行文件和程序集有什么区别/
程序集清单
□ 查看程序集清单
→清空F盘as文件夹中的所有内容
→创建MainClass.cs文件
→把MainClass.cs编译成程序集
→反编译MyDll.dll,在1.txt文件中呈现
ildasm /out:1.txt MyDll.dll
→打开1.txt文件
1.txt
// Metadata version: v4.0.30319
.assembly extern mscorlib
{.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0}.assembly MyDll{.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ).custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
.hash algorithm 0x00008004.ver 0:0:0:0}.module MyDll.dll// MVID: {7BE59AA1-0AE6-426E-B77D-5B85AB4B163F}
.imagebase 0x10000000.file alignment 0x00000200.stackreserve 0x00100000.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x00A00000
// *********** 反汇编完成 ***********************
// 警告: 创建了 Win32 资源文件 1.res
○ .assembly extern mscorlib,不管MainClass.cs中有没有代码,一定会引用mscorlib程序集
○ .assembly MyDll语句块中的内容就是程序集清单,manifest
○ .hash algorithm 0x00008004和.ver 0:0:0:0是程序集清单中2个重要的方面
□ 查看module清单
→把MainClass.cs编译成module
csc /t:module /out:MyModule.netmodule MainClass.cs
→反编译MyModule,在2.txt文件中打开
ildasm /out:2.txt MyModule.netmodule
→打开2.txt文件
2.txt
// Metadata version: v4.0.30319
.assembly extern mscorlib
{.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0}.module MyModule.netmodule// MVID: {17B1ABDD-85D4-42F8-AF6D-07B860FCADEC}
.imagebase 0x10000000.file alignment 0x00000200.stackreserve 0x00100000.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x00260000
.custom ([mscorlib]System.Runtime.CompilerServices.AssemblyAttributesGoHere) instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
// *********** 反汇编完成 ***********************
○ 以上只有mscorlib这个程序集的清单。
DLL和EXE的区别
→删除F盘as文件夹中除了MainClass.cs之外的所有文件
→用记事本打开MainClass.cs文件,修改如下,保存
using System;
class MainClass
{static void Main(){Console.WriteLine("Hello World");
}}
→编译MainClass.cs,生成MainClass.exe文件
csc MainClass.cs
→运行MainiClass.exe
MainClass.exe
→编译MainClass.cs,生成MainClass.dll文件
csc /t:library MainClass.cs
→反编译MainClass.exe,在1.txt文件中显示
ildasm /out:1.txt MainClass.exe
→打开1.txt文件
1.txt
// Metadata version: v4.0.30319
.assembly extern mscorlib
{.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0}.assembly MainClass{.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ).custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
.hash algorithm 0x00008004.ver 0:0:0:0}.module MainClass.exe// MVID: {1FD51DE8-5CD3-4408-AAB2-630AA5482E23}
.imagebase 0x00400000.file alignment 0x00000200.stackreserve 0x00100000.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x003B0000
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit MainClassextends [mscorlib]System.Object{.method private hidebysig static void Main() cil managed{.entrypoint// 代码大小 13 (0xd)
.maxstack 8IL_0000: nopIL_0001: ldstr "Hello World"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)IL_000b: nopIL_000c: ret} // end of method MainClass::Main
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{// 代码大小 7 (0x7)
.maxstack 8IL_0000: ldarg.0IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret} // end of method MainClass::.ctor
} // end of class MainClass
// =============================================================
// *********** 反汇编完成 ***********************
// 警告: 创建了 Win32 资源文件 1.res
→反编译MainClass.dll,在2.txt文件中显示
ildasm /out:2.txt MainClass.dll
→打开2.txt文件
2.txt
// Metadata version: v4.0.30319
.assembly extern mscorlib
{.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0}.assembly MainClass{.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ).custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
.hash algorithm 0x00008004.ver 0:0:0:0}.module MainClass.dll// MVID: {765C3610-5865-4A53-995C-22B24BDCAEDE}
.imagebase 0x10000000.file alignment 0x00000200.stackreserve 0x00100000.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x00370000
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit MainClassextends [mscorlib]System.Object{.method private hidebysig static void Main() cil managed{// 代码大小 13 (0xd)
.maxstack 8IL_0000: nopIL_0001: ldstr "Hello World"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)IL_000b: nopIL_000c: ret} // end of method MainClass::Main
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{// 代码大小 7 (0x7)
.maxstack 8IL_0000: ldarg.0IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret} // end of method MainClass::.ctor
} // end of class MainClass
// =============================================================
// *********** 反汇编完成 ***********************
// 警告: 创建了 Win32 资源文件 2.res
MainClass.exe和MainClass.dll在IL上的不同体现在:
○ EXE有程序的入口店.entrypoint,而DLL没有
○ EXE的modlue名称为MainClass.exe,DLL的module名称为MainClass.dll
“C#程序集系列”包括:
C#程序集系列01,用记事本编写C#,IL代码,用DOS命令编译程序集,运行程序
C#程序集系列02,使用记事本查看可执行程序集的IL代码
C#程序集系列03,引用多个module
C#程序集系列04,在程序集包含多个module的场景下理解关键字internal
C#程序集系列05,让程序集包含多个module
C#程序集系列06,程序集清单,EXE和DLL的区别
C#程序集系列07,篡改程序集
C#程序集系列08,设置程序集版本
C#程序集系列09,程序集签名
C#程序集系列10,强名称程序集
C#程序集系列11,全局程序集缓存
C#程序集系列12,C#编译器和CLR如何找寻程序集
C#程序集系列13,如何让CLR选择不同版本的程序集
参考资料:
http://www.computersciencevideos.org/ created by Jamie King
相关文章
- C# String 前面不足位数补零的方法
- C#基础-获得当前程序的 空间名.类名.方法名
- 中小研发团队架构实践之生产环境诊断工具WinDbg 三分钟学会.NET微服务之Polly 使用.Net Core+IView+Vue集成上传图片功能 Fiddler原理~知多少? ABP框架(asp.net core 2.X+Vue)模板项目学习之路(一) C#程序中设置全局代理(Global Proxy) WCF 4.0 使用说明 如何在IIS上发布,并能正常访问
- 连表查询都用Left Join吧 以Windows服务方式运行.NET Core程序 HTTP和HTTPS的区别 ASP.NET SignalR介绍 asp.net—WebApi跨域 asp.net—自定义轻量级ORM C#之23中设计模式
- C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序
- SQL 横转竖 、竖专横 (转载) 使用Dapper.Contrib 开发.net core程序,兼容多种数据库 C# 读取PDF多级书签 Json.net日期格式化设置 ASPNET 下载共享文件 ASPNET 文件批量下载 递归,循环,尾递归 利用IDisposable接口构建包含非托管资源对象 《.NET 进阶指南》读书笔记2------定义不可改变类型
- 反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) C#中缓存的使用 C#操作redis WPF 控件库——可拖动选项卡的TabControl 【Bootstrap系列】详解Bootstrap-table AutoFac event 和delegate的分别 常见的异步方式async 和 await C# Task用法 c#源码的执行过程
- .Net基础——程序集与CIL HttpClient封装方法 .Net Core 编码规范 C#中invoke和beginInvoke的使用 WebServeice 动态代理类
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
- C#中泛型方法与泛型接口 C#泛型接口 List<IAll> arssr = new List<IAll>(); interface IPerson<T> c# List<接口>小技巧 泛型接口协变逆变的几个问题
- [C#基础]c#中的BeginInvoke和EndEndInvoke
- [C#] 使用 Excel 和 Math.Net 进行曲线拟合和数据预测
- C#【补充】this关键字
- C#【异步操作篇】async/await使用实例
- c#中程序以管理员身份运行的三种办法
- C#根据日期范围过滤IQueryable<T>集合
- C#程序集系列08,设置程序集版本
- c# Unicode
- c# 静态变量
- c# http请求,获取非200时的响应体
- 微软收购Xamarin或许对C#开发者是个好消息
- C# 托管资源与非托管资源
- C#怎么给新建的winform程序添加资源文件夹Resources
- C# 通过Process.Start() 打开程序 置顶方法
- 【实战学习c#】为程序设置版本和帮助信息