c# 類的反射實例 (GetType().Invoke().GetMethod().CreateInstance())

原文:http://www.cnblogs.com/chenwei19/archive/2009/02/04/1384034.htmlhtml

Class1和Form 窗體在同一個命名空間函數

 1 using System;  2 using System.Collections.Generic;  3 using System.Text;  4 namespace fanshetest1  5 {  6     class Class1  7  {  8         private string ab="1";  9         public Class1(string aa) 10  { 11             a = aa; 12  } 13         public int  aa(int x,int y) 14  { 15             return x+y+x+y; 16  } 17         public string bb() 18  { 19             return "bb"; 20  } 21         public static string cc() 22  { 23             return "cc"; 24  } 25         public string AB 26  { 27             get
28  { 29                 return ab; 30  } 31             set
32  { 33                 ab = value; 34  } 35  } 36         public string a; 37  } 38 }

 

Class1和Form 窗體在不一樣一個命名空間this

 1 using System;  2 using System.Collections.Generic;  3 using System.Text;  4 namespace fanshetest1  5 {  6     class Class1  7  {  8         private string ab="1";  9         public Class1(string aa) 10  { 11             a = aa; 12  } 13         public int  aa(int x,int y) 14  { 15             return x+y+x+y; 16  } 17         public string bb() 18  { 19             return "bb"; 20  } 21         public static string cc() 22  { 23             return "cc"; 24  } 25         public string AB 26  { 27             get
28  { 29                 return ab; 30  } 31             set
32  { 33                 ab = value; 34  } 35  } 36         public string a; 37  } 38 }

 

下面是如何使用反射操做以上類;spa

 1 using System;  2 using System.Collections.Generic;  3 using System.ComponentModel;  4 using System.Data;  5 using System.Drawing;  6 using System.Text;  7 using System.Windows.Forms;  8 using System.Reflection;  9 namespace fanshetest  10 {  11     public partial class Form1 : Form  12  {  13  System.Reflection.Assembly a;  14  System.Reflection.Assembly b;  15         public Form1()  16  {  17  InitializeComponent();  18             a = System.Reflection.Assembly.LoadFrom("Class1.DLL");  19  }  20         private void button1_Click(object sender, EventArgs e)  21  {  22  gouzaohanshu();  23            
 24  }  25        //沒有傳參數,返回一個類型;
 26         private void One()  27  {  28             //再另建一個項目,在項目中引用上面生成的ClassLibrary1.DLL
 29             System.Type t = a.GetType("Class1.Class1");  30             //動態生成ClassLibrary1.Class類的實例
 31             Object theObj = System.Activator.CreateInstance(t);  32             //參數信息,GetSum須要兩個string參數
 33             System.Type[] paramTypes = new System.Type[2];  34             paramTypes[0] = System.Type.GetType("System.String");  35             paramTypes[1] = System.Type.GetType("System.String");  36             //System.Reflection.MethodInfo mi = t.GetMethod("aa", paramTypes);
 37             System.Reflection.MethodInfo mi = t.GetMethod("bb");  38             //參數值
 39             Object[] parameters = new Object[2];  40             parameters[0] = 3;  41             parameters[1] = 4;  42             Object returnValue = mi.Invoke(theObj, null);  43             this.textBox1.Text = returnValue.ToString();  44  }  45         //沒有返回值,調用的是直接執行函數
 46         private void None_void()  47  {  48             //再另建一個項目,在項目中引用上面生成的ClassLibrary1.DLL
 49             System.Type t = a.GetType("Class1.Class2");  50             //動態生成ClassLibrary1.Class類的實例
 51             Object theObj = System.Activator.CreateInstance(t);  52             //參數信息,GetSum須要兩個string參數
 53             System.Type[] paramTypes = new System.Type[2];  54             //此處調用方法,若是有參數只須要在括號的後面加上"," 加上參數的type[]類型的參數
 55             System.Reflection.MethodInfo mi = t.GetMethod("Mes");  56             Object returnValue = mi.Invoke(theObj, null);  57  }  58         //沒有返回值,傳出參數接收返回值;
 59         private void Two()  60  {  61             Type t = a.GetType("Class1.Class1");  62             Object theObj = Activator.CreateInstance(t);  63             Type[] types=new Type[2];  64             types[0]=Type.GetType("System.Int32");  65             types[1]=Type.GetType("System.Int32");  66             Object[] obj=new Object[2];  67             obj[0]=2;  68             obj[1]=3;  69             MethodInfo mi = t.GetMethod("aa",types);  70            Object returnValue=mi.Invoke(theObj,obj);  71            this.textBox1.Text = returnValue.ToString();  72  }  73         //獲取同一個命名空間的反射出值
 74         private void Local()  75  {  76             Type t=Type.GetType("fanshetest1.Class1");  77             //建立一個 實例
 78             Object theObj = Activator.CreateInstance(t);  79             Type[] types=new Type[2];  80             types[0]=Type.GetType("System.Int32");  81             types[1] = Type.GetType("System.Int32");  82             Object[] obj = new Object[2];  83             obj[0] = 2;  84             obj[1] = 3;  85             MethodInfo mi = t.GetMethod("aa",types);  86             Object returnValue = mi.Invoke(theObj,obj);  87             this.textBox1.Text = returnValue.ToString();  88  }  89         //獲取出一個屬性
 90         private void attribute()  91  {  92             a = Assembly.LoadFrom("Class1.dll");  93             Type t = a.GetType("Class1.Class1");  94             t = Type.GetType("fanshetest1.Class1");  95             Object theObj = Activator.CreateInstance(t);  96             t.GetProperty("AB").SetValue(theObj,"a",null);  97             this.textBox1.Text = t.GetProperty("AB").GetValue(theObj, null).ToString();  98  }  99         //獲取出靜態的函數;
100         private void static_() 101  { 102             a = System.Reflection.Assembly.LoadFrom("Class1.dll"); 103             Type t = a.GetType("Class1.Class1"); 104             t = Type.GetType("fanshetest1.Class1"); 105             //建立一個實例
106             Object theObj = Activator.CreateInstance(t); 107             //建立一個方法的實例
108             MethodInfo mi = t.GetMethod("cc"); 109             Object returnValue = mi.Invoke(theObj, null); 110             this.textBox1.Text = returnValue.ToString(); 111  } 112         //獲取出變量;
113         private void variable() 114  { 115             a = Assembly.LoadFrom("Class1.dll"); 116             Type t = a.GetType("Class1.Class1"); 117             t = Type.GetType("fanshetest1.Class1"); 118             Object theObj = Activator.CreateInstance(t); 119             MethodInfo mi = t.GetMethod("a"); 120             t.GetField("a").SetValue(theObj,"a"); 121             this.textBox1.Text = t.GetField("a").GetValue(theObj).ToString(); 122  } 123         //獲取出私有變量反射值;
124         private void private_() 125  { 126             
127  } 128         //構造函數
129         private void gouzaohanshu() 130  { 131             Type t = a.GetType("Class1.Class1"); 132             t = Type.GetType("fanshetest1.Class1"); 133             //建立構造函數類型
134             Type[] structure_Type = new Type[1]; 135             structure_Type[0] = Type.GetType("System.String"); 136             //定義構造函數傳參類型
137             Object[] structure_Obj = new Object[1]; 138             structure_Obj[0] = "aa"; 139             //建立一個 實例
140             Object theObj = Activator.CreateInstance(t,structure_Obj); 141             //MethodInfo structure_Mi = t.GetConstructor(structure_Type); 142             //MethodInfo structure_Mi = t.GetMethod("Class1", structure_Type); 143             //structure_Mi.Invoke(theObj, structure_Obj);
144             MethodInfo mi = t.GetMethod("a"); 145             //t.GetField("a").SetValue(theObj, "a");
146             this.textBox1.Text = t.GetField("a").GetValue(theObj).ToString(); 147  } 148  } 149 }
相關文章
相關標籤/搜索