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

_疾跑的蝸牛發表於2018-07-03

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

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 窗體在不同一個名稱空間

 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 }

 

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

  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 }

 

相關文章