用于反射测试的类:
using System;
using System.Collections.Generic;
using System.Text;
namespace HelloReflection
{
public interface IAdd
{
int Add();
}
public abstract class Car
{
private string _carName;
public string CarName
{
get { return _carName; }
set { _carName = value; }
}
public abstract void Run();
}
public class TestClass:IAdd
{
public int Add()
{
return 10;
}
private int _age;
private int Age
{
get { return _age; }
set { _age = value; }
}
public string _smallName;
public string SmallName
{
get { return _smallName; }
set { _smallName = value; }
}
public string _bigName;
public string BigName
{
get { return _bigName; }
set { _bigName = value; }
}
// 构造函数,无参
public TestClass()
{
this._smallName = "小赖";
this._bigName = "大赖";
}
// 构造函数,有参
public TestClass(string smallName, string bigName)
{
this._smallName = smallName;
this._bigName = bigName;
}
// 静态方法,公有,有参
public static string WelcomeEarthPeople(string name)
{
return "用静态有参方法热烈欢迎地球的嘉宾:" + name;
}
// 静态方法,公有,无参
public static string WelcomeJupiterPeople()
{
return "用静态无参方法热烈欢迎木星的嘉宾";
}
// 实例方法,公有,有参
public string WelcomeMoonPeople(string name, int m)
{
return "用实例方法热烈欢迎来自 " + m + " 号月球的嘉宾:" + name;
}
// 实例方法,公有,无参
public string WelcomeSunPeople()
{
return "用无参公用方法欢迎来自太阳的嘉宾";
}
// 实例方法,私有,无参
private string WelcomeMarsPeople()
{
return "用无参私有方法欢迎来自火星的嘉宾";
}
}
}
反射的引用:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using HelloReflection;
/////////////////////////////////////
///Author:Vincent.Lai
///Description:Basic usage of reflection
///Date:2009-05-02
////////////////////////////////////////
namespace ConsoleApplication1
{
/// <summary>
/// The demo is only for the new learner of reflection, so there is some mutiple code.
/// </summary>
class Program
{
delegate string TestDelegate(string value, int value1);
static void Main(string[] args)
{
Assembly testAssembly; //声明一个程序集对象
//testAssembly = Assembly.Load("HelloReflection");
testAssembly = Assembly.LoadFrom("HelloReflection.dll"); //加载程序集
/*******************************得到具体的类的类型***********************************/
Console.WriteLine("\n\n*******************具体的类的类型**********************");
Type testType = typeof(HelloReflection.TestClass); //与下面的效果是一样的
//Type testType = a.GetType("HelloReflection.TestClass");
Console.WriteLine(testType.Name);
/*******************输出该dll下所有的类(包括接口和抽象类)**********************/
Console.WriteLine("**********该dll下所有的类(包括接口和抽象类)**********");
foreach (Type t in testAssembly.GetTypes())
{
Console.WriteLine(t.Name);
}
/*******************************显示模块的名字***********************************/
Console.WriteLine("\n\n*******************模块的名字**********************");
Module[] ms = testAssembly.GetModules();
foreach (Module m in ms)
{
Console.WriteLine("module name : " + m.Name);
}
/*******************************创建类的实例***********************************/
Console.WriteLine("\n\n*******************新创建的类实例**********************");
string[] constructorPara = { "小顺生", "大顺生" };
object objParaInstance = Activator.CreateInstance(testType, constructorPara);//para为构造函数的参数,当然也可以没有参数
//object obj = Activator.CreateInstance(testType);
TestClass tr = null;
checked
{//checked的作用:当要进行类型转换时,如果要捕获转换时的溢出等,则要用checked
tr = (HelloReflection.TestClass)objParaInstance; //强制转换
}
Console.WriteLine( " big name : " + tr.BigName);
/*******************************得到所有公共方法的信息***********************************/
Console.WriteLine("\n\n*******************所有公共方法的信息**********************");
MethodInfo[] methodInfo = testType.GetMethods();
foreach (MethodInfo mi in methodInfo)
{
Console.WriteLine(mi.Name);
}
/*******************************调用无参实例方法***********************************/
Console.WriteLine("\n\n*******************调用无参实例方法**********************");
MethodInfo wsp2 = testType.GetMethod("WelcomeSunPeople");
object objInstance = Activator.CreateInstance(testType); //创建类的实例
string result = (string)wsp2.Invoke(objInstance, null); //WelcomeSunPeople方法没有参数,所以此处用null
Console.WriteLine("WelcomeSunPeople方法的执行结果:" + result);
/*******************************调用有参实例方法***********************************/
Console.WriteLine("\n\n*******************调有无参实例方法**********************");
MethodInfo wmp = testType.GetMethod("WelcomeMoonPeople");
object[] para3 = { "嫦娥", 3 };
string result3 = (string)wmp.Invoke(objInstance, para3); //WelcomeSunPeople方法没有参数,所以此处用null
Console.WriteLine("WelcomeMoonPeople方法的执行结果:" + result3);
/*******************************调用静态有参方法***********************************/
Console.WriteLine("\n\n*******************调有静态有参方法**********************");
MethodInfo wep = testType.GetMethod("WelcomeEarthPeople");
object[] para4 = { "邱桂妹" };
string result4 = (string)wep.Invoke(null, para4);
Console.WriteLine("WelcomeEarthPeople方法执行的结果:" + result4);
/*******************************调用静态无参方法***********************************/
Console.WriteLine("\n\n*******************调有静态无参方法**********************");
MethodInfo wjp = testType.GetMethod("WelcomeJupiterPeople");
string result5 = (string)wjp.Invoke(null, null);
Console.WriteLine("WelcomeJupiterPeople方法执行的结果:" + result5);
/*******************************调用实例私有无参方法***********************************/
Console.WriteLine("\n\n*******************调用实例私有无参方法**********************");
MethodInfo wmsp = testType.GetMethod("WelcomeMarsPeople", BindingFlags.Instance | BindingFlags.NonPublic);
string result6 = (string)wmsp.Invoke(objInstance, null);
Console.WriteLine("WelcomeMarsPeople方法执行的结果:" + result6);
/*******************************显示所有的属性***********************************/
Console.WriteLine("\n\n*******************所有的属性**********************");
PropertyInfo[] piArr = testType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (PropertyInfo pi in piArr)
{
Console.WriteLine(pi.Name); //显示所有的属性
}
/*******************************访问公有的属性***********************************/
Console.WriteLine("\n\n*******************访问公有属性BigName**********************");
PropertyInfo pi1 = testType.GetProperty("BigName", BindingFlags.Instance | BindingFlags.Public);
pi1.SetValue(objInstance, "大大赖", null); //设置属性的值
Console.WriteLine(pi1.GetValue(objInstance, null)); //读取属性的值
/*******************************访问私有的属性***********************************/
Console.WriteLine("\n\n*******************访问私有属性Age**********************");
PropertyInfo pi2 = testType.GetProperty("Age", BindingFlags.Instance | BindingFlags.NonPublic);
pi2.SetValue(objInstance, 24, null); //设置属性的值
Console.Write(pi2.GetValue(objInstance, null)); //读取属性的值
/*******************************访问私有的字段***********************************/
Console.WriteLine("\n\n*******************访问私有字段_age**********************");
FieldInfo fi1 = testType.GetField("_age", BindingFlags.Instance | BindingFlags.NonPublic);
fi1.SetValue(objInstance, 23); //设置字段的值
Console.WriteLine(fi1.GetValue(objInstance)); //读取字段的值
/*******************************访问所有构造函数***********************************/
Console.WriteLine("\n\n*******************获得构造函数的形式**********************");
ConstructorInfo[] ci = testType.GetConstructors();
foreach (ConstructorInfo _ci in ci)
{
Console.WriteLine(_ci.ToString());
}
/*******************************访问某个构造函数***********************************/
Console.WriteLine("\n\n*******************获得某个构造函数的形式**********************");
ConstructorInfo asCI = testType.GetConstructor(new Type[] { typeof(string), typeof(string) }); //TestClass中定义了两个参数的构造函数
Console.Write(asCI.ToString());
/*******************************访问接口***********************************/
Console.WriteLine("\n\n*******************访问接口**********************");
HelloReflection.IAdd interface1 = (HelloReflection.IAdd)testAssembly.CreateInstance("HelloReflection.TestClass");
HelloReflection.TestClass tr12 = (HelloReflection.TestClass)testAssembly.CreateInstance("HelloReflection.TestClass");
Console.WriteLine(interface1.Add());//典型的工厂模式
Console.WriteLine(tr12.Add());
/*******************************访问接口2***********************************/
Console.WriteLine("\n\n*******************访问接口2**********************");
foreach (Type tt in testAssembly.GetTypes())
{
if (tt.GetInterface("IAdd")!=null) //如果实现了接口
{
HelloReflection.IAdd obj13 = (HelloReflection.IAdd)Activator.CreateInstance(testType);
Console.Write(obj13.Add()); //调用接口方法
}
}
/*******************************动态创建委托***********************************/
Console.WriteLine("\n\n*******************动态创建委托**********************");
TestDelegate method = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), objInstance, "WelcomeMoonPeople");
//动态创建委托的简单例子
Console.WriteLine(method("天蓬元帅", 2));
/*******************************通过无参构造函数创建对象***********************************/
Console.WriteLine("\n\n*******************通过无参构造函数创建对象**********************");
ConstructorInfo asCI1 = testType.GetConstructor(new Type[0]);
HelloReflection.TestClass obj14 = (HelloReflection.TestClass)asCI1.Invoke(null);
Console.WriteLine("BigName: " + obj14.BigName + " SmallName:"+obj14.SmallName); // 创建对象
/*******************************通过无参构造函数创建对象***********************************/
Console.WriteLine("\n\n*******************通过有参构造函数创建对象**********************");
ConstructorInfo asCI2 = testType.GetConstructor(new Type[] { typeof(string), typeof(string) });
string[] para14 = { "张3","张三"};
HelloReflection.TestClass obj15 = (HelloReflection.TestClass)asCI2.Invoke(para14); //创建对象
Console.WriteLine("BigName: " + obj15.BigName + " SmallName:" + obj15.SmallName);
Console.Read();
}
}
}