被反射的代码类
using System;
using System.Collections.Generic;
using System.Text;
namespace ReflectionSample
{
/// <summary>
/// 参考博客园,测试并改进
/// </summary>
public class ClassSample
{
/// <summary>
/// 无参数构造函数
/// </summary>
public ClassSample()
{
this.name = "这个是默认构造函数(无参数)。";
}
/// <summary>
/// 带参数构造函数
/// </summary>
/// <param name="name"></param>
public ClassSample(string name)
{
this.name = name;
}
/// <summary>
/// 字段
/// </summary>
public string name;
public string Field;
private string property;
/// <summary>
/// 属性
/// </summary>
public string Property
{
set { this.property = value; }
get { return property; }
}
/// <summary>
/// Public 方法
/// </summary>
/// <returns></returns>
public string PublicClassMethod()
{
return "Public方法返回值";
}
/// <summary>
/// Private 方法
/// </summary>
/// <returns></returns>
private string PrivateClassMethod()
{
return "Private方法返回值";
}
/// <summary>
/// Static 方法
/// </summary>
/// <returns></returns>
public static string StaticMethod()
{
return "Static方法返回值";
}
/// <summary>
/// 带参数方法
/// </summary>
/// <param name="para"></param>
/// <returns></returns>
public string ParameterMethod(string para)
{
return "这个是带参数的方法:" + para;
}
public event EventHandler eventHandler;
public void DoEvent()
{
eventHandler(null, EventArgs.Empty);
}
}
}
反射方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.DirectoryServices;
using System.Reflection;
namespace ReflectionSample
{
/// <summary>
/// 参考博客园,测试并改进
/// </summary>
class Program
{
static void Main(string[] args)
{
Assembly classSampleAssembly = null;
//string filePath = "./xxx.dll"; // 程序集完整路径
// 获取程序集
// classSampleAssembly = Assembly.LoadFrom(filePath); // 加载其他程序集的方式
classSampleAssembly = Assembly.GetExecutingAssembly(); // 加载相同程序集的方式
// 从程序集中获取指定对象类型
Type classSampleType = classSampleAssembly.GetType("ReflectionSample.ClassSample"); // 程序集名.类名
#region 使用Activator创建一个实例
// 通过对象类型创建对象实例
ClassSample s1 = Activator.CreateInstance(classSampleType) as ClassSample;
Console.WriteLine(s1.name + "使用Activator创建一个实例");
#endregion
#region 动态调用构造函数
// 调用无参构造
ConstructorInfo studentConstructor1 = classSampleType.GetConstructor(new Type[] { });
ClassSample s2 = studentConstructor1.Invoke(new object[] { }) as ClassSample;
Console.WriteLine("您调用了无参构造创建了一个类实例:" + s2.name);
// 调用有参构造
ConstructorInfo studentConstructor2 = classSampleType.GetConstructor(new Type[] { typeof(string) });
ClassSample s3 = studentConstructor2.Invoke(new object[] { "这个是有参数构造函数。" }) as ClassSample;
Console.WriteLine("您调用了有参构造创建了一个类实例:" + s3.name);
#endregion
#region 反射方法
// 调用非静态方法
string returnValue1 = classSampleType.InvokeMember("PublicClassMethod",
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
null, s1,
new object[] { })
as string;
Console.WriteLine("调用非静态方法执行结果:" + returnValue1);
// 调用静态方法
string returnValue2 = classSampleType.InvokeMember("StaticMethod", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, s1, new object[] { }) as string;
Console.WriteLine("调用静态方法执行结果:" + returnValue2);
// 调用私有方法
string returnValue3 = classSampleType.InvokeMember("PrivateClassMethod",
BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
null,
s1,
new object[] { }) as string;
Console.WriteLine("调用私有方法执行结果:" + returnValue3);
#endregion
#region 反射参数
MethodInfo parameterMethod = classSampleType.GetMethod("ParameterMethod");
ParameterInfo[] paras = parameterMethod.GetParameters();
for (int i = 0; i < paras.Count(); i++)
{
Console.WriteLine(string.Format("参数 {0} :{1}", i, paras[i].Name));
}
#endregion
#region 反射属性
// 设置属性的值
classSampleType.InvokeMember("Property",
BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance,
null,
s1,
new object[] { "设置属性的值" });
// 获取属性的值
string returnValue4 = classSampleType.InvokeMember("Property",
BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance,
null,
s1,
new object[] { }) as string;
Console.WriteLine("反射属性:" + returnValue4);
#endregion
#region 反射字段
// 设置字段的值
classSampleType.InvokeMember("Field",
BindingFlags.SetField | BindingFlags.Public | BindingFlags.Instance,
null,
s1,
new object[] { "设置字段的值" });
// 获取字段的值
string returnValue5 = classSampleType.InvokeMember("Field",
BindingFlags.GetField | BindingFlags.Public | BindingFlags.Instance,
null,
s1,
new object[] { }) as string;
Console.WriteLine("反射字段:" + returnValue5);
#endregion
Console.WriteLine("请按回车键结束.");
Console.ReadLine();
}
}
}