反射示例1 - - - - - -执行反射dll的方法和获取属性的值

  

项目结构:

反射示例1 - - - - - -执行反射dll的方法和获取属性的值

将文档类库生成的dll文件放在ConsoleApplication2 \ ConsoleApplication2 \ bin \调试文件夹下

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -文档。cs

 using 系统;
  using  System.Collections.Generic;
  using 来;
  using 包含;
  using  System.Threading.Tasks;
  using  System.Reflection;
  using 先;
  namespace  ConsoleApplication2
  {
  ,,,class 程序
  ,,,{
  ,,,,,,,static  void  Main (string [], args)
  ,,,,,,,{
  ,,,,,,,,,,,//获取程序集
  ,,,,,,,,,,,Assembly  asb =, Assembly.LoadFrom (Directory.GetCurrentDirectory (), +,/Document.dll);
  ,,,,,,,,,,,//获取程序集下面类的文档
  ,,,,,,,,,,,Type  document =, asb.GetType (“Document.Document”);
  ,,,,,,,,,,,//实例化文档类对象(有参数的话需要传递对象参数)
  ,,,,,,,,,,,object  docObj =, Activator.CreateInstance(文档);
  ,,,,,,,,,,,//获取文档类中的方法
  ,,,,,,,,,,,MethodInfo  mi =, document.GetMethod (GetName);
  ,,,,,,,,,,,//参数
  ,,,,,,,,,,,对象[],parameter =, new 对象[],{,”张三”,};
  ,,,,,,,,,,,Console.WriteLine (mi.Invoke (docObj,参数).ToString());//输出方法的返回值:张三
  ,,,,,,,,,,,//获取文档类中的属性
  ,,,,,,,,,,,FieldInfo  pi =, document.GetField(“名字”);
  ,,,,,,,,,,,Console.WriteLine (pi.GetValue (docObj));//输出方法的属性:李四
  ,,,,,,,,,,,Console.ReadKey ();
  ,,,,,,,}
  ,,,}
  }

反射示例1 - - - - - -执行反射dll的方法和获取属性的值