C# Assembly
Assembly
一、简介
程序集是代码进行编译是的一个逻辑单元,把相关的代码和类型进行组合,然后生成PE文件。程序集只是逻辑上的划分,一个程序集可以只由一个文件组成,也可由多个文件组成。不管是单文件程序集还是多文件程序集,它们都由固定的结构组成
常见的两种程序集:
可执行文件(.exe文件)和 类库文件(.dll文件)。
在VS开发环境中,一个解决方案可以包含多个项目,而每个项目就是一个程序集。
他们之间是一种从属关系,也就是说,一个AppDomain能够包括N个Assembly,一个Assembly能够包括N个Module,一个Module能够包括N个Type,一个Type能够包括N个成员。他们都在System.Reflection命名空间下。【公共语言运行库CLR】加载器 管理 应用程序域,这种管理包括 将每个程序集加载到相应的应用程序域 以及 控制每个程序集中类型层次结构的内存布局。
模块是可移植的可执行文件,例如 type.dll 或 application.exe,由一个或多个类和接口组成。 单个模块可包含多个命名空间,而一个命名空间可跨越多个模块。
二、Assembly程序集对象
1.获得当前对象所属的类所在的程序集
this.GetType().Assembly;
Type对象肯定在一个assembly对象中
可以通过Type对象得到程序集。
2.根据路径加载程序集
Assembly.LoadFrom(assPath);
3.获得当前【应用程序域】中的所有程序集
Assembly[] ass = AppDomain.CurrentDomain.GetAssemblies();
三、Type类型对象
Type 是一个抽象类,我们用的都是TypeInfo类的对象。
程序运行时,一个class对应一个Type类的对象。通过Type对象可以获得类的所有信息。
1. 通过类获得对应的Type
Type t1 = typeof(Person);
2. 通过对象获得Type
Type t2 = person.GetType(); this.GetType();
3.用assembly对象,通过类的full name类获得type对象
Assembly ass1 = Assembly.LoadFrom(@"c:\users\xcao\documents\visual studio 2012\Projects\ConsoleApplication6\ConsoleApplication6\libs\Ecole.dll");
//GetType的参数一定要是full name的string
Type tStu = ass1.GetType("Ecole.Student");
object stu1 = Activator.CreateInstance(tStu);
4.获得程序集中定义的所有的public类
Type[] allPublicTypes = ass1.GetExportedTypes();
5.获得程序集中定义的所有的类
Type[] allTypes = ass1.GetTypes();
6.通过Assembly创建类
6.1 RefClass 辅助类
using System;
using System.Collections.Generic;
using System.Text;
namespace _01
{
public class RefClass
{
private int _test3;
private int _test1 { get; set; }
protected int Test2 { get; set; }
public int Test3 { get; set; }
public void Show()
{
Console.WriteLine("hhhh");
}
}
}
6.2 Program
using System;
using System.Reflection;
namespace _01
{
internal class Program
{
static void Main(string[] args)
{
//加载程序集这里是01
Assembly ass = Assembly.Load("01");
//遍历程序集里所有Type的全名
foreach (var a in ass.GetTypes())
{
Console.WriteLine("Name: " + a.Name);
Console.WriteLine("FullName: " + a.FullName);
}
//GetType要使用FullName,使用Name不起效
Type type1 = ass.GetType("_01.RefClass");
//Activator通过类型创建实体
object refClass1 = Activator.CreateInstance(type1);
//强制转换为RefClass
RefClass refClass2 = (RefClass)refClass1;
refClass2.Show();
Console.ReadKey();
}
}
}
7.Type类的属性:
t.Assembly; 获取t所在的程序集
t.FullName; 获取t所对应的类的full name
t.Name; 获取t所对应的类的 name
t.IsArray; 判断t是否是一个数组类
t.IsEnum; 判断t是否是一个枚举类
t.IsAbstract; 判断t是否是一个抽象类
t.IsInterface; 判断t是否是一个interface
8.Type类的方法:
notebookInterfaceType.IsAssignableFrom(Type t);判断t是否实现了 notebookInterfaceType 接口
t.IsSubclassOf(Type parent); t是否是parent的子类
t.IsInstanceOfType(object o); o是否是t类的对象
t.GetFields(); //method, property 得到所有的public的fields,methods,properties
t.GetField("gender"); 根据名字得到某个field
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人