您的位置:首页 > 编程语言 > C#

C# 反射学习

2016-01-07 17:48 519 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace ReflectionStudy
{
public partial class Form1 : Form
{
/*
反射技术
反射(Reflection)是.NET中的重要机制,通过反射,可以在运行时获得.NET中每一个类型(包括类、结构、委托、接口和枚举等)的成员,包括方法、属性、事件,以及构造函数等。
还可以获得每个成员的名称、限定符和参数等。有了反射,即可对每一个类型了如指掌。如果获得了构造函数的信息,即可直接创建对象,即使这个对象的类型在编译时还不知道。
反射通常具有以下用途。
(1)使用Assembly定义和加载程序集,加载在程序集清单中列出模块,以及从此程序集中查找类型并创建该类型的实例。
(2)使用Module了解包含模块的程序集以及模块中的类等,还可以获取在模块上定义的所有全局方法或其他特定的非全局方法。
(3)使用ConstructorInfo了解构造函数的名称、参数、访问修饰符(如pulic 或private)和实现详细信息(如abstract或virtual)等。使用Type的GetConstructors或GetConstructor方法来调用特定的构造函数。
(4)使用MethodInfo了解方法的名称、返回类型、参数、访问修饰符(如pulic 或private)和实现详细信息(如abstract或virtual)等。使用Type的GetMethods或GetMethod方法来调用特定的方法。
(5)使用FiedInfo了解字段的名称、访问修饰符(如public或private)和实现详细信息(如static)等,并获取或设置字段值。
(6)使用EventInfo了解事件的名称、事件处理程序数据类型、自定义属性、声明类型和反射类型等,添加或移除事件处理程序。
(7)使用PropertyInfo了解属性的名称、数据类型、声明类型、反射类型和只读或可写状态等,获取或设置属性值。
(8)使用ParameterInfo了解参数的名称、数据类型、是输入参数还是输出参数,以及参数在方法签名中的位置等。
System.Reflection.Emit命名空间的类提供了一种特殊形式的反射,可以在运行时构造类型。
反射也可用于创建称为类型浏览器的应用程序,使用户能够选择类型,然后查看有关选定类型的信息。
此外,Jscript等语言编译器使用反射来构造符号表。System.Runtime.Serialization命名空间中的类使用反射来访问数据并确定要永久保存的字段,System.Runtime.Remoting命名空间中的类通过序列化来间接地使用反射。
*/
public Form1()
{
InitializeComponent();
LoadAssembly();
}

System.Reflection.MethodInfo mInfo = null;
System.Reflection.EventInfo eventInfo = null;
object instance = null;
Type[] typeDefine = null;
private void LoadAssembly()
{
System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom("CSStudy.dll");
Type type = assembly.GetType("CSStudy.Reflection");

//******************************带一个参数的方法***********************************
typeDefine = new Type[1];
typeDefine[0] = Type.GetType("System.String");
mInfo = type.GetMethod("WriteString", typeDefine);
//定义构造函数的参数
object[] param = new object[1];
param[0] = "apple";
//动态生成实例
instance = System.Activator.CreateInstance(type, param);
string GetWriteStringValue = mInfo.Invoke(instance, new object[] { "有参数方法WriteString" }).ToString();

//******************************带两个参数的方法***********************************
typeDefine = new Type[2];
typeDefine[0] = Type.GetType("System.String");
typeDefine[1] = Type.GetType("System.Int32");
mInfo = type.GetMethod("WriteString", typeDefine);
//instance = assembly.CreateInstance("CSStudy.Reflection");
object[] objs = new object[2];
objs[0] = "有参数方法WriteString";
objs[1] = 222;
string GetWriteStringValue1 = mInfo.Invoke(instance, objs).ToString();

//******************************静态方法的调用************************************
object[] staticValue = new object[1];
staticValue[0] = "静态方法WriteName";
mInfo = type.GetMethod("WriteName");
string returnStatic = mInfo.Invoke(null, staticValue).ToString();

//******************************返回无参方法的值***********************************
mInfo = type.GetMethod("WriteNoPara");
string returnNullParamMethod = mInfo.Invoke(instance, null).ToString();

//*************************设置或获取属性的值,字段的值*****************************
//设置属性的值
//type.GetProperty("AB").SetValue(instance,"ABC", null);
//获取属性的值
string property = type.GetProperty("AB").GetValue(instance, null).ToString();
//获取字段的值
string field = type.GetField("a").GetValue(instance).ToString();

//********************反射事件类型和绑定事件处理程序*******************************
mInfo = type.GetMethod("StartEvent", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
eventInfo = type.GetEvent("EventHandle");
eventInfo.AddEventHandler(instance, new EventHandler(GetEventHandle));
mInfo.Invoke(instance, null);

//*************************反射委托类型创建委托实例********************************
mInfo = type.GetMethod("StartParamsEvent", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
Type delType = type.GetNestedType("DeleMethod");
Delegate del = Delegate.CreateDelegate(delType, instance, mInfo);
object objec = del.DynamicInvoke(110);

//********************反射事件类型和创建委托实例**********************************
mInfo = type.GetMethod("ParamsEvents", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
delType = type.GetNestedType("DeleMethod");
eventInfo = type.GetEvent("ParamsEvent");
del = Delegate.CreateDelegate(delType, this, "GetDelResult");
eventInfo.AddEventHandler(instance, del);
mInfo.Invoke(instance, new object[] { 119 });
}

public void GetEventHandle(object sender, EventArgs e)
{
string str = "事件返回结果";
}

public string GetDelResult(int i)
{
string result = i.ToString();
return result;
}
}

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