.net C# 反射技术使用例子

  获得实例的方法

  1.获得同一程序集内的类型的实例。

  无参数构造函数

  Type t = Type.GetType("AppCode.Employe");

  if (t == null)

  {

  textBox1.Text = "Create Type Error";

  return;

  }

  object emp = t.Assembly.CreateInstance("AppCode.Employe");

  if (emp == null)

  {

  textBox1.Text = "Create Instance Error";

  return;

  }

  StringBuilder sb = new StringBuilder();

  sb.AppendLine("Type:" + t.FullName);

  sb.AppendLine("Context:");

  sb.AppendLine(emp.ToString());

  textBox1.Text = sb.ToString();

  有参数构造函数

  Type t = Type.GetType("AppCode.Employe");

  if (t == null)

  {

  textBox1.Text = "Create Type Error";

  return;

  }

  //构造函数参数列表如下

  //public Employe(string userid, string username, int age)

  //生成参数类型数组

  Type[] ts = new Type[3] { typeof(System.String),typeof(System.String), typeof(System.Int16) };

  //获得构造函数

  ConstructorInfo mi = t.GetConstructor(ts);

  if (mi == null)

  {

  textBox1.Text = "Create ConstructorInfo Error";

  return;

  }

  //生成参数值数组

  object[] ps=new object[3];

  ps[0] = "420106780911326";

  ps[1] = "wesker";

  ps[2] = 36;

  object emp=mi.Invoke(ps);

  StringBuilder sb = new StringBuilder();

  sb.AppendLine("Type:" + t.FullName);

  sb.AppendLine("Context:");

  sb.AppendLine(emp.ToString());

  textBox1.Text = sb.ToString();

  2.获得不同程序集内的类型的实例。

  比如当前窗体程序要通过反射获得一个名为"compenent1"(文件名为compenent1.dll)的程序集中的类型的实例。

  无参数构造函数

  Type t = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "\\compenent1.dll").

  GetType("compenent1.User");

  if (t == null)

  {

  textBox1.Text = "Create Type Error";

  return;

  }

  object user = t.Assembly.CreateInstance("compenent1.User");

  if (user == null)

  {

  textBox1.Text = "Create Instance Error";

  return;

  }

  StringBuilder sb = new StringBuilder();

  sb.AppendLine("Type:" + t.FullName);

  sb.AppendLine("Context:");

  sb.AppendLine(user.ToString());

  textBox1.Text = sb.ToString();

  有参数的构造函数

  Type t = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "\\compenent1.dll").

  GetType("compenent1.User");

  if (t == null)

  {

  textBox1.Text = "Create Type Error";

  return;

  }

  //构造函数参数列表如下

  //public Employe(string userid, string username, int age)

  Type[] ts = new Type[3] { typeof(System.String), typeof(System.String), typeof(System.Int16) };

  ConstructorInfo mi = t.GetConstructor(ts);

  if (mi == null)

  {

  textBox1.Text = "Create ConstructorInfo Error";

  return;

  }

  object[] ps = new object[3];

  hdyei.com

  www.hsdje.com

  hsdje.com

  www.ieuen.com

  ieuen.com

  www.iiyjg.com

  iiyjg.com

  www.iuqwo.com

  iuqwo.com

  www.kauwc.com

  kauwc.com

  www.kodij.com

  kodij.com

  www.mcjdo.com

  mcjdo.com

  www.mkvjf.com

  mkvjf.com

  www.mkzjs.com

  mkzjs.com

  www.njoww.com

  njoww.com

  www.pqkud.com

  pqkud.com

  www.qonxh.com

  qonxh.com

  www.svcej.com

  svcej.com

  www.trruc.com

  trruc.com

  www.ycfdi.com

  ycfdi.com

  www.yrtwx.com

  yrtwx.com

  www.ahude.com

  ahude.com

  www.bgayw.com

  bgayw.com

  www.bhodc.com

  bhodc.com

  www.bhyds.com

  bhyds.com

  www.eoplc.com

  eoplc.com

  www.hufhs.com

  hufhs.com

  www.jiwjf.com

  jiwjf.com

  www.lodlc.com

  lodlc.com

  www.lovkv.com

  lovkv.com

  www.njgif.com

  njgif.com

  www.qpwoc.com

  qpwoc.com

  www.riodk.com

  riodk.com

  www.slodk.com

  slodk.com

  www.uhjio.com

  uhjio.com

  www.xlpwd.com

  xlpwd.com

  ps[0] = "420106780911326";

  ps[1] = "wesker";

  ps[2] = 36;

  object user = mi.Invoke(ps);

  StringBuilder sb = new StringBuilder();

  sb.AppendLine("Type:" + t.FullName);

  sb.AppendLine("Context:");

  sb.AppendLine(user.ToString());

  textBox1.Text = sb.ToString();

  3.使用反射调用方法

  Type t = Type.GetType("AppCode.Employe");

  if (t == null)

  {

  textBox1.Text = "Create Type Error";

  return;

  }

  object emp = t.Assembly.CreateInstance("AppCode.Employe");

  if (emp == null)

  {

  textBox1.Text = "Create Instance Error";

  return;

  }

  MethodInfo mi = t.GetMethod("ToString");

  string context = mi.Invoke(emp,null).ToString();

  StringBuilder sb = new StringBuilder();

  sb.AppendLine("Context:");

  sb.AppendLine(context);

  sb.AppendLine("Type:" + t.FullName);

  textBox1.Text = sb.ToString();

  4.获得类的信息

  通过Type.GetMethods方法获得方法的信息。

  通过Type.GetConstructors获得构造函数。

  5.使用Activator类实例化类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值