c# 反射初探【转】

首先新建一个类库,将它生成ClassLibrary1.dll

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4.   
  5. namespace WebTest   
  6. {   
  7.     public class ReflectTest   
  8.     {   
  9.         private string word;   
  10.   
  11.         public String Word   
  12.         {   
  13.             get  
  14.             {   
  15.                 return word;   
  16.             }   
  17.             set  
  18.             {   
  19.                 word = value;   
  20.             }   
  21.         }   
  22.   
  23.         public ReflectTest(string w)   
  24.         {   
  25.             this.word = w;   
  26.         }   
  27.   
  28.         public ReflectTest()   
  29.         {   
  30.             this.word = "word";   
  31.         }   
  32.   
  33.         public string WriteString(String s)   
  34.         {   
  35.             return "Welcome + " + s;   
  36.         }   
  37.   
  38.         public static string WriteStatic(String name)   
  39.         {   
  40.             return "Static : Welcome + " + name;   
  41.         }   
  42.   
  43.         public string WriteNoPara()   
  44.         {   
  45.             return "NoPara : Welcome !";   
  46.         }   
  47.         private String WritePrivate()   
  48.         {   
  49.             return "Private ";   
  50.         }   
  51.     }   
  52.     public class TestClass   
  53.     {   
  54.     }   
  55. }  

 

然后新建立一个项目以入该dll(代码里有e文注释)

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4. using System.Reflection;   
  5.   
  6. namespace TestReflect   
  7. {   
  8.     class Program   
  9.     {   
  10.         static void Main(string[] args)   
  11.         {   
  12.             System.Reflection.Assembly ass;   
  13.             Type type;   
  14.             Object obj;   
  15.             try  
  16.             {   
  17.                 ass = Assembly.Load("ClassLibrary1");   
  18.   
  19.                 Console.WriteLine("-----------All the class In this dll-----------");   
  20.                 foreach (Type t in ass.GetTypes())   
  21.                 {   
  22.                     Console.WriteLine(t.Name);   
  23.                 }   
  24.   
  25.                 Console.WriteLine("---------All the Module In this dll----------");   
  26.                 Module[] miar = ass.GetModules();   
  27.                 foreach (Module m in miar)   
  28.                 {   
  29.                     Console.WriteLine(m.Name);   
  30.                 }   
  31.   
  32.                 type = ass.GetType("WebTest.ReflectTest");   
  33.   
  34.                 Console.WriteLine("-----------All the method In WebTest.ReflectTest-----------");   
  35.                 MethodInfo[] miarr = type.GetMethods();   
  36.                 foreach (MethodInfo m in miarr)   
  37.                 {   
  38.                     Console.WriteLine(m.Name);   
  39.                 }   
  40.   
  41.                 Console.WriteLine("-----------All the Property In WebTest.ReflectTest-----------");   
  42.                 PropertyInfo[] piarr = type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);   
  43.                 foreach (PropertyInfo p in piarr)   
  44.                 {   
  45.                     Console.WriteLine(p.Name);   
  46.                 }   
  47.   
  48.                 Console.WriteLine("----------Call some methods in WebTest.ReflectTest-----------");   
  49.                 obj = ass.CreateInstance("WebTest.ReflectTest");   
  50.   
  51.                 MethodInfo method = type.GetMethod("WriteString");   
  52.                 String s = (String)method.Invoke(obj, new string[] { "WriteString" });   
  53.                 Console.WriteLine(s);   
  54.   
  55.                 method = type.GetMethod("WriteStatic");   
  56.                 s = (String)method.Invoke(nullnew string[] { "WriteName" });   
  57.                 Console.WriteLine(s);   
  58.   
  59.                 method = type.GetMethod("WriteNoPara");   
  60.                 s = (String)method.Invoke(obj, null);   
  61.                 Console.WriteLine(s);   
  62.   
  63.                 method = type.GetMethod("WritePrivate", BindingFlags.Instance | BindingFlags.NonPublic);   
  64.                 s = (String)method.Invoke(obj, null);   
  65.                 Console.WriteLine(s);   
  66.   
  67.                 Console.WriteLine("---------Use the Property---------");   
  68.                 PropertyInfo pi = type.GetProperty("Word");   
  69.                 pi.SetValue(obj, "Word"null);   
  70.                 Console.WriteLine(pi.GetValue(obj,null));   
  71.   
  72.                 Console.WriteLine("--------- Get All Constructor ----------");   
  73.                 ConstructorInfo[] ci = type.GetConstructors();   
  74.                 foreach (ConstructorInfo c in ci)   
  75.                 {   
  76.                     Console.WriteLine(c.ToString());   
  77.                 }   
  78.   
  79.                 Console.WriteLine("-------- Construct --------");   
  80.                 ConstructorInfo ci1 = type.GetConstructor(new Type[] { typeof(string) });   
  81.                 WebTest.ReflectTest wr1 = (WebTest.ReflectTest)ci1.Invoke(new string[] { "XXX" });   
  82.                 Console.WriteLine(wr1.Word);   
  83.   
  84.                 ConstructorInfo ci2 = type.GetConstructor(new Type[0] );   
  85.                 WebTest.ReflectTest wr2 = (WebTest.ReflectTest)ci2.Invoke(null);   
  86.                 Console.WriteLine(wr2.Word);   
  87.   
  88.             }   
  89.             catch (Exception ex)   
  90.             {   
  91.                 Console.WriteLine(ex);   
  92.             }   
  93.             finally  
  94.             {   
  95.                 ass = null;   
  96.                 type = null;   
  97.                 obj = null;   
  98.             }   
  99.   
  100.         }   
  101.     }   
  102. }  

 

谢谢.

posted on 2009-10-21 20:39  ToKens  阅读(321)  评论(0编辑  收藏  举报