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

Class1和Form 窗体在同一个命名空间

 using System;
using System.Collections.Generic;
using System.Text;
namespace fanshetest1
{
class Class1
{
private string ab="";
public Class1(string aa)
{
a = aa;
}
public int aa(int x,int y)
{
return x+y+x+y;
}
public string bb()
{
return "bb";
}
public static string cc()
{
return "cc";
}
public string AB
{
get
{
return ab;
}
set
{
ab = value;
}
}
public string a;
}
}

Class1和Form 窗体在不同一个命名空间

 using System;
using System.Collections.Generic;
using System.Text;
namespace fanshetest1
{
class Class1
{
private string ab="";
public Class1(string aa)
{
a = aa;
}
public int aa(int x,int y)
{
return x+y+x+y;
}
public string bb()
{
return "bb";
}
public static string cc()
{
return "cc";
}
public string AB
{
get
{
return ab;
}
set
{
ab = value;
}
}
public string a;
}
}

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

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace fanshetest
{
public partial class Form1 : Form
{
System.Reflection.Assembly a;
System.Reflection.Assembly b;
public Form1()
{
InitializeComponent();
a = System.Reflection.Assembly.LoadFrom("Class1.DLL");
}
private void button1_Click(object sender, EventArgs e)
{
gouzaohanshu(); }
//没有传参数,返回一个类型;
private void One()
{
//再另建一个项目,在项目中引用上面生成的ClassLibrary1.DLL
System.Type t = a.GetType("Class1.Class1");
//动态生成ClassLibrary1.Class类的实例
Object theObj = System.Activator.CreateInstance(t);
//参数信息,GetSum需要两个string参数
System.Type[] paramTypes = new System.Type[];
paramTypes[] = System.Type.GetType("System.String");
paramTypes[] = System.Type.GetType("System.String");
//System.Reflection.MethodInfo mi = t.GetMethod("aa", paramTypes);
System.Reflection.MethodInfo mi = t.GetMethod("bb");
//参数值
Object[] parameters = new Object[];
parameters[] = ;
parameters[] = ;
Object returnValue = mi.Invoke(theObj, null);
this.textBox1.Text = returnValue.ToString();
}
//没有返回值,调用的是直接执行函数
private void None_void()
{
//再另建一个项目,在项目中引用上面生成的ClassLibrary1.DLL
System.Type t = a.GetType("Class1.Class2");
//动态生成ClassLibrary1.Class类的实例
Object theObj = System.Activator.CreateInstance(t);
//参数信息,GetSum需要两个string参数
System.Type[] paramTypes = new System.Type[];
//此处调用方法,如果有参数只需要在括号的后面加上"," 加上参数的type[]类型的参数
System.Reflection.MethodInfo mi = t.GetMethod("Mes");
Object returnValue = mi.Invoke(theObj, null);
}
//没有返回值,传出参数接收返回值;
private void Two()
{
Type t = a.GetType("Class1.Class1");
Object theObj = Activator.CreateInstance(t);
Type[] types=new Type[];
types[]=Type.GetType("System.Int32");
types[]=Type.GetType("System.Int32");
Object[] obj=new Object[];
obj[]=;
obj[]=;
MethodInfo mi = t.GetMethod("aa",types);
Object returnValue=mi.Invoke(theObj,obj);
this.textBox1.Text = returnValue.ToString();
}
//获取同一个命名空间的反射出值
private void Local()
{
Type t=Type.GetType("fanshetest1.Class1");
//创建一个 实例
Object theObj = Activator.CreateInstance(t);
Type[] types=new Type[];
types[]=Type.GetType("System.Int32");
types[] = Type.GetType("System.Int32");
Object[] obj = new Object[];
obj[] = ;
obj[] = ;
MethodInfo mi = t.GetMethod("aa",types);
Object returnValue = mi.Invoke(theObj,obj);
this.textBox1.Text = returnValue.ToString();
}
//获取出一个属性
private void attribute()
{
a = Assembly.LoadFrom("Class1.dll");
Type t = a.GetType("Class1.Class1");
t = Type.GetType("fanshetest1.Class1");
Object theObj = Activator.CreateInstance(t);
t.GetProperty("AB").SetValue(theObj,"a",null);
this.textBox1.Text = t.GetProperty("AB").GetValue(theObj, null).ToString();
}
//获取出静态的函数;
private void static_()
{
a = System.Reflection.Assembly.LoadFrom("Class1.dll");
Type t = a.GetType("Class1.Class1");
t = Type.GetType("fanshetest1.Class1");
//创建一个实例
Object theObj = Activator.CreateInstance(t);
//创建一个方法的实例
MethodInfo mi = t.GetMethod("cc");
Object returnValue = mi.Invoke(theObj, null);
this.textBox1.Text = returnValue.ToString();
}
//获取出变量;
private void variable()
{
a = Assembly.LoadFrom("Class1.dll");
Type t = a.GetType("Class1.Class1");
t = Type.GetType("fanshetest1.Class1");
Object theObj = Activator.CreateInstance(t);
MethodInfo mi = t.GetMethod("a");
t.GetField("a").SetValue(theObj,"a");
this.textBox1.Text = t.GetField("a").GetValue(theObj).ToString();
}
//获取出私有变量反射值;
private void private_()
{ }
//构造函数
private void gouzaohanshu()
{
Type t = a.GetType("Class1.Class1");
t = Type.GetType("fanshetest1.Class1");
//创建构造函数类型
Type[] structure_Type = new Type[];
structure_Type[] = Type.GetType("System.String");
//定义构造函数传参类型
Object[] structure_Obj = new Object[];
structure_Obj[] = "aa";
//创建一个 实例
Object theObj = Activator.CreateInstance(t,structure_Obj);
//MethodInfo structure_Mi = t.GetConstructor(structure_Type);
//MethodInfo structure_Mi = t.GetMethod("Class1", structure_Type);
//structure_Mi.Invoke(theObj, structure_Obj);
MethodInfo mi = t.GetMethod("a");
//t.GetField("a").SetValue(theObj, "a");
this.textBox1.Text = t.GetField("a").GetValue(theObj).ToString();
}
}
}

最新文章

  1. 移动端上传照片 预览+Draw on Canvas's Demo(解决 iOS 等设备照片旋转 90 度的 bug)
  2. 转:HTTP 1.1与HTTP 1.0的比较
  3. 使用eclipse开发Java web应用
  4. 图像处理工具包ImagXpress中如何定义图像显示属性
  5. 【原】创建Hive表,分号分隔符“;”引起的异常
  6. 268条PCB Layout设计规范
  7. POJ2236 Wireless Network 并查集
  8. wuzhicms模版语法
  9. anglehack参赛总结
  10. tensorflow softmax应用
  11. [BZOJ1606] [Usaco2008 Dec] Hay For Sale 购买干草 (dp)
  12. 词向量:part 1 WordNet、SoW、BoW、TF-IDF、Hash Trick、共现矩阵、SVD
  13. 处理SQL Server中的重复行
  14. 什么是 Message Queue
  15. 【图像处理】openCV库教程
  16. 简明的Python教程中的几个疑惑点分析#2
  17. 杭电1024----Max Sum Plus Plus
  18. Nhibernate入门篇连接Sqlserver的增删查改
  19. 【ASP.Net】 web api中的media type
  20. Angularjs 中的 controller

热门文章

  1. delphi IOS 通知 TNotification
  2. FFmpeg多媒体文件格式探测
  3. Form表单标签的Enctype属性的作用及应用示例介绍
  4. PHP 数组中出现中文乱码,json_encode返回结果为null 或false
  5. axis2 webService开发指南(2)
  6. Linux 安装elasticsearch、node.js、elasticsearch-head
  7. 274. H-Index论文引用量
  8. mybatis中使用懒加载实现一对多复杂查询
  9. shiro 集成spring 使用 redis作为缓存 学习记录(六)
  10. Java代码加密与反编译(二):用加密算法DES修改classLoader实现对.class文件加密