zoukankan      html  css  js  c++  java
  • C#运用反射调用其他程序集中的代码

    加载程序集

    AssMedicalAdvice = Assembly.LoadFrom(Path.Combine(Environment.CurrentDirectory, "Inscription.MedicalAdvice.Client.dll"));

     1.调用静态函数,静态函数不需要实例化类,所以methodInfo.Invoke第一个参数为null

     type = AssClinicalPaths.GetType("Inscription.ClinicalPaths.Client.CommonCtl.RasonV2.CP_RASON_GridFormV2");
     methodInfo = type.GetMethod("Test");
     methodInfo.Invoke(null, new string[] { });

     2.获取静态属性

     type = AssClinicalLib.GetType("Inscription.ClinicalPaths.Lib.LibManager");
     string strDbType = type.GetProperty("strDBType").GetValue(null, null).ToString();
     string strDbConn = type.GetProperty("strDBConn").GetValue(null, null).ToString();

    3.获取实例属性

     type = AssMedicalLib.GetType("Inscription.MedicalAdvice.Object.BaseSreverManager");
     
     object objBaseServerManager = type.GetProperty("sBaseSreverManager").GetValue(null, null);
     strDbType = type.GetProperty("DBType").GetValue(objBaseServerManager, null).ToString();
     strDbConn = type.GetProperty("DBConn").GetValue(objBaseServerManager, null).ToString();

     4.调用非静态函数,需要获取PathManager类的实例化。

      或者使用Activator.CreateInstance(type)实例化类

     type = AssClinicalPaths.GetType("Inscription.ClinicalPaths.Lib.PathManager");
    //Object objPathManager = Activator.CreateInstance(type); Object objPathManager
    = type.GetProperty("pathManager").GetValue(null, null); methodInfo = type.GetMethod("in_RunTimeObject"); methodInfo.Invoke(objPathManager, new string[] { });

    PS:反射有个有趣的特性,就是能够访问私有的字段,属性和方法。

    有点类似代码级别的Hack行为。不知道这算不算一个好的OO设计,毕竟他破坏了类的封装性。

  • 相关阅读:
    Unity 摄像机旋转跟随缩放控制
    Unity 协程深入解析与原理
    好看的滚动条
    ES6编译问题SyntaxError: Unexpected token import
    Axure rp8 注册码,亲测可以用! 可用给个赞呗!!
    angular 项目中遇到rxjs error TS1005:';'
    window 查看端口 杀端口
    angular 中嵌套 iframe 报错
    js 快速生成数组的方法
    ng-packagr 不能全部打包文件
  • 原文地址:https://www.cnblogs.com/linuxjava01/p/3729831.html
Copyright © 2011-2022 走看看