关于C#放射的入门搜集整理
反射(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命名空间中的类通过序列化来间接地使用反射。
简单举例:
1 创建用于反射使用的DLL
新建一个C#类库项目,拷贝源代码如下,编译生成DLL(假如DLL的文件名是TestReflect.dll)
 using System;
using System;2

3
 namespace Webtest
namespace Webtest4
 {
{5
 /// <summary>
 /// <summary>6
 /// ReflectTest 的摘要说明。
 /// ReflectTest 的摘要说明。7
 /// </summary>
 /// </summary>8
 public class ReflectTest
 public class ReflectTest9
 {
 {10
 public ReflectTest()
  public ReflectTest()11
 {}
  {}12

13
 public string WriteString(string s)
  public string WriteString(string s)14
 {
  {15
 return "欢迎您," + s;
   return "欢迎您," + s;16
 }
  }17

18
 /// <summary>
  /// <summary>19
 /// dsajkjflasjdfalksdjfaskfd
  /// dsajkjflasjdfalksdjfaskfd20
 /// </summary>
  /// </summary>21
 /// <param name="s"></param>
  /// <param name="s"></param>22
 /// <returns></returns>
  /// <returns></returns>23
 public static string WriteName(string s)
  public static string WriteName(string s)24
 {
  {25
 return "欢迎您光临," + s;
   return "欢迎您光临," + s;26
 }
  }27

28
 public string WriteNoPara()
  public string WriteNoPara()29
 {
  {30
 return "您使用的是无参数方法";
   return "您使用的是无参数方法";31
 }
  }32
 }
 }33
 }
}34

35

36

 
2 应用于反射的例子
在ASPNET页面中加入以下函数:
 public void test1()
public void test1()2
 {
  {3
 System.Reflection.Assembly ass;
   System.Reflection.Assembly ass;4
 Type type ;
   Type type ;5
 object obj;
   object obj;6
 try
   try7
 {
   {8
 ass = System.Reflection.Assembly.LoadFile(@"d:\TestReflect.dll");
    ass = System.Reflection.Assembly.LoadFile(@"d:\TestReflect.dll");9
 type = ass.GetType("Webtest.ReflectTest");//必须使用名称空间+类名称
    type = ass.GetType("Webtest.ReflectTest");//必须使用名称空间+类名称10
 System.Reflection.MethodInfo method = type.GetMethod("WriteString");//方法的名称
    System.Reflection.MethodInfo method = type.GetMethod("WriteString");//方法的名称11
 obj = ass.CreateInstance("Webtest.ReflectTest");//必须使用名称空间+类名称
    obj = ass.CreateInstance("Webtest.ReflectTest");//必须使用名称空间+类名称12
 string s = (string)method.Invoke(obj,new string[]{"jianglijun"}); //实例方法的调用
    string s = (string)method.Invoke(obj,new string[]{"jianglijun"}); //实例方法的调用13
 
   14
 Response.Write(s+"<br>");
    Response.Write(s+"<br>");15
 method = type.GetMethod("WriteName");//方法的名称
    method = type.GetMethod("WriteName");//方法的名称16
 s = (string)method.Invoke(null,new string[]{"jianglijun"}); //静态方法的调用
    s = (string)method.Invoke(null,new string[]{"jianglijun"}); //静态方法的调用17
 Response.Write(s+"<br>");
    Response.Write(s+"<br>");18

19
 method = type.GetMethod("WriteNoPara");//无参数的实例方法
    method = type.GetMethod("WriteNoPara");//无参数的实例方法20
 s = (string)method.Invoke(obj,null);
    s = (string)method.Invoke(obj,null);21
 Response.Write(s+"<br>");
    Response.Write(s+"<br>");22
 method = null;
    method = null;23
 }
   }24
 catch(Exception ex)
   catch(Exception ex)25
 {
   {26
 Response.Write(ex+"<br>");
    Response.Write(ex+"<br>");27
 }
   }28
 finally
   finally29
 {
   {30
 ass = null;
    ass = null;31
 type = null;
    type = null;32
 obj = null;
    obj = null;33
 }
   }34
 }
  }35

复杂举例:
在网上查找了不少的资料,可以说大同小异,概念性的东西网上一搜一堆,
今天把反射的东西整理了一下,供大家使用,我保证我这里是最全面的东西,当然也是基础的东西,
在学好了这一切的基础上,大家可以学习反射的具体插件等应用,老鸟就不用看了.
首先我们建立一个类库,将它生成为HelloWorld.dll,
 using System;
using System;
 namespace Webtest
 namespace Webtest {
 {
 public interface interface1
    public interface interface1 {
     { int add();
          int add(); 
      }
     } public class ReflectTest:interface1
     public class ReflectTest:interface1 {
     { 
          public String Write;
         public String Write; private String Writec;
         private String Writec;
 public String Writea
         public String Writea {
         { get
             get {
             { return Write;
                 return Write; }
             } set
             set {
             { Write = value;
                 Write = value; }
             } 
          }
         }
 private String Writeb
         private String Writeb {
         { get
             get {
             { return Writec;
                 return Writec; }
             } set
             set {
             { Writec = value;
                 Writec = value; }
             }
 }
         }
 public ReflectTest()
          public ReflectTest() {
          { this.Write = "Write";
              this.Write = "Write"; this.Writec = "Writec";
              this.Writec = "Writec"; }
          }
 public ReflectTest(string str1,string str2)
         public ReflectTest(string str1,string str2) {
         { this.Write = str1;
             this.Write = str1; this.Writec = str2;
             this.Writec = str2; }
         }
 public string WriteString(string s,int b)
         public string WriteString(string s,int b) {
         { return "欢迎您," + s + "---" + b; ;
             return "欢迎您," + s + "---" + b; ; }
         }
 public static string WriteName(string s)
          public static string WriteName(string s) {
          { return "欢迎您光临," + s;
             return "欢迎您光临," + s; }
          }
 public string WriteNoPara()
         public string WriteNoPara() {
         { return "您使用的是无参数方法";
            return "您使用的是无参数方法"; }
         }
 private string WritePrivate()
         private string WritePrivate() {
         { return "私有类型的方法";
             return "私有类型的方法"; }
         }

 public int add()
         public int add() {
         { return 5;
             return 5; }
         } }
     } }
}然后,建立再建立一个项目引入该HelloWorld.dll,
 using System;
using System;
 using System.Threading;
using System.Threading; using System.Reflection;
using System.Reflection;

 class Test
class Test {
{ delegate string TestDelegate(string value,int value1);
    delegate string TestDelegate(string value,int value1);
 static void Main()
   static void Main() {
    { //Assembly t = Assembly.LoadFrom("HelloWorld.dll"); 与下面相同的效果
        //Assembly t = Assembly.LoadFrom("HelloWorld.dll"); 与下面相同的效果 Assembly t = Assembly.Load("HelloWorld");
        Assembly t = Assembly.Load("HelloWorld");
 //**********************************************************************
//**********************************************************************      foreach (Type aaa in t.GetTypes())
       foreach (Type aaa in t.GetTypes()) {
       { //Console.Write(aaa.Name);   //显示该dll下所有的类
            //Console.Write(aaa.Name);   //显示该dll下所有的类 }
        }
 //**********************************************************************
//********************************************************************** Module[] modules = t.GetModules();
        Module[] modules = t.GetModules();
 foreach (Module module in modules)
        foreach (Module module in modules) {
        { //Console.WriteLine("module name:" + module.Name);//显示模块的名字本例为"HelloWorld.dll"
            //Console.WriteLine("module name:" + module.Name);//显示模块的名字本例为"HelloWorld.dll" }
        }
 //**********************************************************************
//********************************************************************** Type a = typeof(Webtest.ReflectTest);//得到具体的类的类型,和下面一个效果
        Type a = typeof(Webtest.ReflectTest);//得到具体的类的类型,和下面一个效果 //Type a = t.GetType("Webtest.ReflectTest");//
        //Type a = t.GetType("Webtest.ReflectTest");// //Console.Write(a.Name);
        //Console.Write(a.Name);
 //**********************************************************************
//********************************************************************** string[] bb ={ "aaaa", "bbbbb" };
        string[] bb ={ "aaaa", "bbbbb" }; object obj = Activator.CreateInstance(a,bb); //创建该类的实例,后面的bb为有参构造函数的参数
        object obj = Activator.CreateInstance(a,bb); //创建该类的实例,后面的bb为有参构造函数的参数 //object obj = t.CreateInstance("Webtest.ReflectTest");//与上面方法相同
        //object obj = t.CreateInstance("Webtest.ReflectTest");//与上面方法相同
 //**********************************************************************
//**********************************************************************         MethodInfo[] miArr = a.GetMethods();
        MethodInfo[] miArr = a.GetMethods(); foreach (MethodInfo mi0 in miArr)
        foreach (MethodInfo mi0 in miArr) {
       { //Console.Write(mi0.Name);  //显示所有的共有方法
            //Console.Write(mi0.Name);  //显示所有的共有方法 }
       }
 //**********************************************************************
//********************************************************************** MethodInfo mi = a.GetMethod("WriteString");//显示具体的方法
        MethodInfo mi = a.GetMethod("WriteString");//显示具体的方法 object[] aa={"使用的是带有参数的非静态方法",2};
        object[] aa={"使用的是带有参数的非静态方法",2}; string s = (string)mi.Invoke(obj,aa); //带参数方法的调用
        string s = (string)mi.Invoke(obj,aa); //带参数方法的调用
 MethodInfo mi1 = a.GetMethod("WriteName");
        MethodInfo mi1 = a.GetMethod("WriteName"); String[] aa1 ={"使用的是静态方法"};
        String[] aa1 ={"使用的是静态方法"}; string s1 = (string)mi1.Invoke(null, aa1); //静态方法的调用
        string s1 = (string)mi1.Invoke(null, aa1); //静态方法的调用
 MethodInfo mi2 = a.GetMethod("WriteNoPara");
        MethodInfo mi2 = a.GetMethod("WriteNoPara"); string s2 = (string)mi2.Invoke(obj, null); //不带参数的方法调用
        string s2 = (string)mi2.Invoke(obj, null); //不带参数的方法调用
 MethodInfo mi3 = a.GetMethod("WritePrivate",BindingFlags.Instance | BindingFlags.NonPublic);
        MethodInfo mi3 = a.GetMethod("WritePrivate",BindingFlags.Instance | BindingFlags.NonPublic); string s3 = (string)mi3.Invoke(obj, null); //私有类型方法调用
        string s3 = (string)mi3.Invoke(obj, null); //私有类型方法调用
 //Console.Write(s3);
        //Console.Write(s3);
 //**********************************************************************
//********************************************************************** PropertyInfo[] piArr = a.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        PropertyInfo[] piArr = a.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); foreach (PropertyInfo pi in piArr)
        foreach (PropertyInfo pi in piArr) {
        { //Console.Write(pi.Name);  //显示所有的属性
         //Console.Write(pi.Name);  //显示所有的属性 }
        }
 //**********************************************************************
//********************************************************************** PropertyInfo pi1=a.GetProperty("Writea");
        PropertyInfo pi1=a.GetProperty("Writea"); //pi1.SetValue(obj, "Writea", null);
        //pi1.SetValue(obj, "Writea", null); //Console.Write(pi1.GetValue(obj,null));
        //Console.Write(pi1.GetValue(obj,null));
 PropertyInfo pi2 = a.GetProperty("Writeb", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        PropertyInfo pi2 = a.GetProperty("Writeb", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); pi2.SetValue(obj, "Writeb", null);
        pi2.SetValue(obj, "Writeb", null); //Console.Write(pi2.GetValue(obj, null));
        //Console.Write(pi2.GetValue(obj, null));
 FieldInfo fi1 = a.GetField("Write");
        FieldInfo fi1 = a.GetField("Write"); //Console.Write(fi1.GetValue(obj));
        //Console.Write(fi1.GetValue(obj));
 //**********************************************************************
//********************************************************************** ConstructorInfo[] ci1 = a.GetConstructors();
        ConstructorInfo[] ci1 = a.GetConstructors(); foreach (ConstructorInfo ci in ci1)
        foreach (ConstructorInfo ci in ci1) {
        { //Console.Write(ci.ToString()); //获得构造函数的形式
            //Console.Write(ci.ToString()); //获得构造函数的形式 }
        }
 ConstructorInfo asCI = a.GetConstructor(new Type[] { typeof(string), typeof(string) });
        ConstructorInfo asCI = a.GetConstructor(new Type[] { typeof(string), typeof(string) }); //Console.Write(asCI.ToString());
        //Console.Write(asCI.ToString());
 //**********************************************************************
//********************************************************************** Webtest.interface1 obj1 = (Webtest.interface1)t.CreateInstance("Webtest.ReflectTest");
        Webtest.interface1 obj1 = (Webtest.interface1)t.CreateInstance("Webtest.ReflectTest"); Webtest.ReflectTest obj2 = (Webtest.ReflectTest)t.CreateInstance("Webtest.ReflectTest");
        Webtest.ReflectTest obj2 = (Webtest.ReflectTest)t.CreateInstance("Webtest.ReflectTest"); //Console.Write(obj1.add());典型的工厂模式
        //Console.Write(obj1.add());典型的工厂模式
 //**********************************************************************
//********************************************************************** foreach (Type tt in t.GetTypes())
        foreach (Type tt in t.GetTypes()) {
        { if (tt.GetInterface("interface1")!=null)
            if (tt.GetInterface("interface1")!=null) {
            { Webtest.interface1 obj3 = (Webtest.interface1)Activator.CreateInstance(a);
                Webtest.interface1 obj3 = (Webtest.interface1)Activator.CreateInstance(a); //Console.Write(obj3.add());
                //Console.Write(obj3.add()); }
            } }
        }
 //**********************************************************************
//********************************************************************** TestDelegate method = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), obj, "WriteString");
        TestDelegate method = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), obj, "WriteString");//动态创建委托的简单例子
 //Console.Write(method("str1", 2));
        //Console.Write(method("str1", 2));
 //**********************************************************************
//********************************************************************** ConstructorInfo asCI1 = a.GetConstructor(new Type[0]);
        ConstructorInfo asCI1 = a.GetConstructor(new Type[0]); Webtest.ReflectTest obj5 = (Webtest.ReflectTest)asCI1.Invoke(null);
        Webtest.ReflectTest obj5 = (Webtest.ReflectTest)asCI1.Invoke(null);//通过无参构造函数实例化的方法
 //Console.Write(obj5.Writea);
        //Console.Write(obj5.Writea);
 ConstructorInfo asCI2 = a.GetConstructor(new Type[] { typeof(string), typeof(string) });
        ConstructorInfo asCI2 = a.GetConstructor(new Type[] { typeof(string), typeof(string) });//通过有参构造函数实例化的方法
 Webtest.ReflectTest obj6 = (Webtest.ReflectTest)asCI2.Invoke(bb);
        Webtest.ReflectTest obj6 = (Webtest.ReflectTest)asCI2.Invoke(bb); Console.Write(obj6.Writea);
        Console.Write(obj6.Writea); //**********************************************************************
//**********************************************************************
 Console.Read();
        Console.Read(); }
    }    }
}
在这里我把我们常用的方法,属性,等全部整理了出来,大家不要嫌弃乱,静下心来,自己按照我的分隔一部分一部分的来,保证你对反射的学习,会事半功倍.当然有关于其方法我会继续补充,想了这么些就先写下来吧.
 
                    
                     
                    
                 
                    
                 


 
  
  
      
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号