您的位置:首页 > 其它

怎样反射程序集?

2015-07-06 14:51 489 查看

第七部分:反射程序集

1、什么是程序集?

1、程序集是.net中的概念。

2、.net中的dll与exe文件都是程序集。(exe与dll的区别?)

3、程序集(Assembly),可以看做是一堆相关类打一个包,相当于java中的jar包(*)。

4、程序集包含:类型元数据(描述在代码中定义的每一类型和成员,二进制形式)、程序集元数据(程序集清单、版本号、名称等)、IL代码(这些都被装在exe或dll中)、资源文件。每个程序集都有自己的名称、版本等信息。这些信息可以通过AssemblyInfo.cs文件来自己定义。

5、使用程序集的好处?

1)、程序中只引用必须的程序集,减小程序的尺寸。

2)、程序集可以封装一些代码,只提供必要的访问接口。

6、如何添加程序集的引用?

1)、添加路径、项目引用、GAC(全局程序集缓存)

不能循环添加引用

在c#中添加其他语言编写的dll文件的引用。(参考P/Invoke,在.net中调用非程序集的dll)extern

dll→ abbr. 动态连接库(=dynamic link library)

GAC的详细配置参考:http://support.microsoft.com/kb/815808/zh-cn

GAC目录C:\WINDOWS\assembly

数字签名(防止引用的程序集被篡改)

我们调用的类都是位于各个程序集中,如果调用的类在没有引用的程序集中,则需要添加对那个程序集的引用,比如ConfigurationManager。

AppDomain.CurrentDomain.GetAssemblies()

2、反射

1、反射无处不在,我们天天在使用。Vs的智能提示,就是通过反射获取到类的属性、方法等。还有反编译工具也是通过反射实现

2、反射就是动态获取程序集的元数据(提供程序集的类型信息)的功能

反射:就是动态获取程序集中的元数据来操作类型的。

3、Type类实现反射的一个重要的类,通过它我们可以获取类中的所有信息包括方法、属性等。可以动态调用类的属性、方法。

(怎样使用反射,这就需要一个重要的类Type类)

4、 Type是对类的描述。如何获取Person类中的所有属性?

5、反射就是直接通过.dll来创建对象,调用成员。

先通过一个普通类介绍Type.(Person)

6、通过类型元数据来获取对象的一些相关信息,并且还可以实例化对象调用方法等,这个就叫做“反射”。

7、反射让创建对象的方式发生了改变。

8、编译器的智能提示就是反射的一个应用。

反射:简单的理解就是通过类型元数据创建对象、调用对象的成员等。

案例1::通过代码验证:Type类来获取程序内部一个Person类中的方法属性;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Reflection;//引进这个程序集

namespace 反射
{

class Program
{

static void Main(string[] args)
{

Person p = new Person();

Type tp = p.GetType()//第二种写法,结果是一样的

Type tp = typeof(Person);//第一种写法

MethodInfo[] methes = tp.GetMethods();//鼠标经过发现是返回一个数组,定义接收

//拿到数组,就可以遍历

for (int i = 0; i < methes.Length; i++)

{

Console.WriteLine(methes[i].Name);

}

Console.WriteLine("================================");

//可以直接获取person这个类中的属性

PropertyInfo[] ps=tp.GetProperties();

//拿到数组就遍历

for (int i = 0; i < ps.Length; i++)

{

Console.WriteLine(ps[i].Name);

}

Console.ReadKey();

}

}

class Person

{

private string _Name;  //Ctrl+ R+封装字段

public string Name

{

get { return _Name; }

set { _Name = value; }

}

private int _Age;

public int Age

{

get { return _Age; }

set { _Age = value; }

}

public void DoSth()

{

}

}

}


案例2::通过代码验证:Type类来获取程序外部一个Person类中的方法属性;



前提是:外部类必须是 public 类型的.......

外部类使用之前先F6生成!!

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Reflection;//引进这个程序集

namespace 反射

{

class Program

{

static void Main(string[] args)

{

string path = @"C:\Users\Administrator\Desktop\ASPNET\反射\MyClass\bin\Debug\MyClass.dll";

Assembly ass = Assembly.LoadFile(path);//加载程序集

Type[] tps = ass.GetExportedTypes();//获取公共的tyep

for (int i = 0; i < tps.Length; i++)

{

Console.WriteLine( "外部dll文件的类名字是这么多:"+tps[i].Name);

Console.WriteLine();

MethodInfo[] ms = tps[i].GetMethods();

int n = 0;

for (int j = 0; j < ms.Length; j++)

{

n++;

Console.WriteLine("外部类长的成员第"+n+"个是:"+ms[j].Name);

}

}

Console.ReadKey();

}

}

}


MyClass子类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyClass
{

public class Person
{

private string _Name;  //Ctrl+ R+封装字段

public string Name
{
get { return _Name; }

set { _Name = value; }
}

private int _Age;

public int Age
{
get { return _Age; }

set { _Age = value; }
}
public void DoSth()
{
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: