欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 类的反射实例 (GetType().Invoke().GetMethod().CreateInstance())

程序员文章站 2022-06-30 22:14:11
原文:http://www.cnblogs.com/chenwei19/archive/2009/02/04/1384034.html Class1和Form 窗体在同一个命名空间 Class1和Form 窗体在不同一个命名空间 下面是如何使用反射操作以上类; ......

原文: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 }