C#的反射
来源:互联网 发布:国产手机性能知乎 编辑:程序博客网 时间:2024/06/05 10:37
反射是一个程序集发现及运行的过程,通过反射可以得到*.exe或*.dll等程序集内部的信息。使用反射可以看到一个程序集内部的接口、类、方法、字段、属性、特性等等信息。在System.Reflection命名空间内包含多个反射常用的类,下面表格列出了常用的几个类。
类型 作用
Assembly 通过此类可以加载操纵一个程序集,并获取程序集内部信息
EventInfo 该类保存给定的事件信息
FieldInfo 该类保存给定的字段信息
MethodInfo 该类保存给定的方法信息
MemberInfo 该类是一个基类,它定义了EventInfo、FieldInfo、MethodInfo、PropertyInfo的多个公用行为
Module 该类可以使你能访问多个程序集中的给定模块
ParameterInfo 该类保存给定的参数信息
PropertyInfo 该类保存给定的属性信息
一、System.Reflection.Assembly类
通过Assembly可以动态加载程序集,并查看程序集的内部信息,其中最常用的就是Load()这个方法。
Assembly assembly=Assembly.Load("MyAssembly");
利用Assembly的objectCreateInstance(string) 方法可以反射创建一个对象,参数0为类名。
二、System.Type类
Type是最常用到的类,通过Type可以得到一个类的内部信息,也可以通过它反射创建一个对象。一般有三个常用的方法可得到Type对象。
利用typeof() 得到Type对象
Type type=typeof(Example);
利用System.Object.GetType() 得到Type对象
Example example=new Example();
Type type=example.GetType();
利用System.Type.GetType() 得到Type对象
Typetype=Type.GetType("MyAssembly.Example",false,true);
注意参数0是类名,参数1表示若找不到对应类时是否抛出异常,参数1表示类名是否区分大小写
例子:
我们最常见的是利用反射与Activator结合来创建对象。
Assembly assembly= Assembly.Load("MyAssembly");
Type type=assembly.GetType("Example");
object obj=Activator.CreateInstance(type);
三、反射方法
1.通过 System.Reflection.MethodInfo能查找到类里面的方法
代码:
Type type=typeof(Example);
MethodInfo[] listMethodInfo=type.GetMethods();
foreach(MethodInfo methodInfo in listMethodInfo)
Cosole.WriteLine("Method name is "+methodInfo.Name);
2.我们也能通过反射方法执行类里面的方法
代码:
Assembly assembly= Assembly.Load("MyAssembly");
Type type=assembly.GetType("Example");
object obj=Activator.CreateInstance(type);
MethodInfo methodInfo=type.GetMethod("Hello World"); //根据方法名获取MethodInfo对象
methodInfo.Invoke(obj,null); //参数1类型为object[],代表HelloWorld方法的对应参数,输入值为null代表没有参数
四、反射属性
1.通过System.Reflection.PropertyInfo 能查找到类里面的属性
常用的方法有GetValue(object,object[])获取属性值和 SetValue(object,object,object[]) 设置属性值
代码:
Type type=typeof(Example);
PropertyInfo[] listPropertyInfo=type.GetProperties();
foreach(PropertyInfo propertyInfo in listPropertyInfo)
Cosole.WriteLine("Property name is "+ propertyInfo.Name);
2.我们也可以通过以下方法设置或者获取一个对象的属性值
代码:
Assembly assembly=Assembly.Load("MyAssembly");
Type type=assembly.GetType("Example");
object obj=Activator.CreateInstance(type);
PropertyInfopropertyInfo=obj.GetProperty("Name"); //获取Name属性对象
var name=propertyInfo.GetValue(obj,null); //获取Name属性的值
PropertyInfo propertyInfo2=obj.GetProperty("Age"); //获取Age属性对象
propertyInfo.SetValue(obj,34,null); //把Age属性设置为34
五、反射字段
通过System.Reflection.FieldInfo 能查找到类里面的字段
它包括有两个常用方法SetValue(object,object )和GetValue(object) 因为使用方法与反射属性非常相似,在此不再多作介绍
(略)
六、反射特性
通过System.Reflection.MemberInfo的GetCustomAttributes(Type,bool)就可反射出一个类里面的特性,以下例子可以反射出一个类的所有特性
代码:
Type type=typeof("Example");
object[] typeAttributes=type.GetCustomAttributes(false); //获取Example类的特性
foreach(object attribute in typeAttributes)
Console.WriteLine("Attributes description is"+attribute.ToString());
通过下面例子,可以获取Example类Name属性的所有特性
代码:
public class Example
{
[DataMemberAttribute]
publics string Name
{get;set;}
..................
}
Type type = typeof(Example);
PropertyInfo propertyInfo=type.GetProperty("Name"); //获取Example类的Name属性
foreach (object attribute inpropertyInfo.GetCustomAttributes(false)) //遍历Name属性的所有特性
Console.WriteLine(“Property attribute:"+attribute.ToString());
七、常用实例
建立一个类库,生成DLL文件。
public class ReflectTest { public ReflectTest() { } public string WriteString(string s) { return "欢迎您," + s; } public static string WriteName(string s) { return "欢迎您光临," + s; } public string WriteNoPara() { return "您使用的是无参数的方法"; } }
把建立的dll文件拷到D盘下,其他的目录页可以。不过下面程序路径要修改一下。
class Program { public void test() { System.Reflection.Assembly ass; Type type ; object obj; try { ass = System.Reflection.Assembly.LoadFile(@"d:\webtest.dll"); type = ass.GetType("webtest.ReflectTest");//必须使用名称空间+类名称 System.Reflection.MethodInfo method = type.GetMethod("WriteString");//方法的名称 obj = ass.CreateInstance("webtest.ReflectTest");//必须使用名称空间+类名称 string s = (string)method.Invoke(obj,new string[]{"jianglijun"}); //实例方法的调用 Console.Write(s + "<br>"); method = type.GetMethod("WriteName");//方法的名称 s = (string)method.Invoke(null,new string[]{"jianglijun"}); //静态方法的调用 Console.Write(s + "<br>"); method = type.GetMethod("WriteNoPara");//无参数的实例方法 s = (string)method.Invoke(obj,null); Console.Write(s + "<br>"); method = null; } catch(Exception ex) { Console.Write(ex + "<br>"); } finally { ass = null; type = null; obj = null; } } static void Main(string[] args) { Program pr = new Program(); pr.test(); Console.Read(); } }
到此结束,谢谢
参阅:http://www.cnblogs.com/leslies2/archive/2011/11/22/2257703.html
- c#的反射
- c#的反射
- c#的反射
- C#反射的体会
- c#的反射
- c#的反射
- c# 反射的用法
- C#反射的用法
- c# 反射的用法
- C#反射的用法
- c# 反射的用法
- C#的反射机制
- C#的反射机制
- c#反射的学习
- C# 反射的用法
- 转 c#的反射
- c# 反射的用法
- c# 反射的用法
- C常见文件操作
- HDU 1072Nightmare(BFS+剪枝)
- 定制高端新闻营销网站推广方式
- 怎样用C++Builder存取Excel
- leetcode : Unique Paths
- C#的反射
- BlueTooth: 蓝牙基础
- IIS的安装于网站架设(For asp.net4.0)
- 设计模式六大原则
- 在Linux下灵活使用expect脚本的小窍门
- SQL Service 查找表列中的属性
- 软件设计师必备——软件工程·建模
- c++ 内存泄漏(Detected memory leaks)解决
- 交换机zone 的概念 和交换机指令