代码改变世界

反射学习总结

2008-11-15 19:46  yearN  阅读(486)  评论(8编辑  收藏  举报

反射是C#中比较重要,也是比较难理解的一部分。反射中主要用到的类有:Type类、Assembly类、Module类,有时候还用到了如ConstructorInfo类、MethodInfo类、MemberInfo类、FieldInfo类、PropertyInfo类、EventInfo类、ParameterInfo类等。使用反射用到的命名空间一般是:System.Reflection,有时候还有System.Reflection.Emit。
在学习反射的过程中,有几个算是新概念:程序集、模块、类型、成员之间的关系。
程序集包含模块,而模块包含类型,类型又包含成员。反射则提供了封装程序集、模块和类型的对象。您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。
示例:
比如,我们要反射一个”HelloWorld”程序集:
Type t;
Assembly a;
t=Assembly.LoadFrom(“HelloWorld.dll”);
我们还可以这样写:
t=Assembly.Load(“HelloWorld”);
我们要得到具体类的类型:
a=t.GetType(“WebTest.ReflectTest”);//WebTest命名空间下ReflectTest类
创建实例:
object obj = Activator.CreateInstance(a, bb); //创建该类的实例,后面的bb为有参构造函数的参数。
还可以这样写:
object obj = t.CreateInstance("Webtest.ReflectTest");
//一般情况为了方便,我们通常这样写:
Assembly.Load(“HelloWorld”).CreateInstance(“Webtest.ReflectTest”);
1.得到dll下的所有类:
foreach (Type aaa in t.GetTypes())
{
//Console.Write(aaa.Name); //显示该dll下所有的类
this.textBox1.Text = this.textBox1.Text + " " + aaa.Name;
}
2.得到模块的名字:
Module[] modules = t.GetModules();
foreach (Module module in modules)
{
//Console.WriteLine("module name:" + module.Name);
//显示模块的名字本例为"HelloWorld.dll"
this.textBox1.Text = this.textBox1.Text + " " + module.Name;
}
3.得到具体的类型:
Type a=t.GetType(“WebTest.ReflectTest”);
Console.Write(a.Name);
4.得到所有方法:
Type a = t.GetType("Webtest.ReflectTest");//得到具体的类的类型
MethodInfo[] miArr = a.GetMethods();
foreach (MethodInfo mi0 in miArr)
{
//Console.Write(mi0.Name); //显示所有的共有方法
this.textBox1.Text = this.textBox1.Text + " "+mi0.Name;
}
5.得到构造函数:
ConstructorInfo[] ci1=a.GetConstructors();
foreach (ConstructorInfo ci in ci1)
{
//Console.Write(ci.ToString()); //获得构造函数的形式
this.textBox1.Text = this.textBox1.Text +" "+ ci.ToString();
}
6.动态创建委托的简单例子:
delegate string TestDelegate(string value, int value1);
object obj = t.CreateInstance("Webtest.ReflectTest");//与上面方法相同
TestDelegate method = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), obj, "WriteString");
Console.Write(method("str1", 2));
7.构造函数实例化:
ConstructorInfo asCI1 = a.GetConstructor(new Type[0]);
Webtest.ReflectTest obj5 = (Webtest.ReflectTest)asCI1.Invoke(null);//通过无参构造函数实例化的方法
Console.Write(obj5.Writea);
string[] bb = { "aaaa", "bbbbb" };
ConstructorInfo asCI2 = a.GetConstructor(new Type[]{ typeof(string), typeof(string) });//通过有参构造函数实例化的方法
Webtest.ReflectTest obj6 = (Webtest.ReflectTest)asCI2.Invoke(bb);
Console.Write(obj6.Writea);

WebTest.ReflectTest代码:
using System;

namespace Webtest
{

public interface interface1
{
int add();

}
public class ReflectTest:interface1
{

public String Write;
private String Writec;

public String Writea
{
get
{
return Write;
}
set
{
Write = value;
}

}

private String Writeb
{
get
{
return Writec;
}
set
{
Writec = value;
}

}

public ReflectTest()
{
this.Write = "Write";
this.Writec = "Writec";
}

public ReflectTest(string str1,string str2)
{
this.Write = str1;
this.Writec = str2;
}

public string WriteString(string s,int b)
{
return "欢迎您," + s + "---" + b; ;
}

public static string WriteName(string s)
{
return "欢迎您光临," + s;
}

public string WriteNoPara()
{
return "您使用的是无参数方法";
}

private string WritePrivate()
{
return "私有类型的方法";
}

 

public int add()
{
return 5;
}
}
}