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设计,毕竟他破坏了类的封装性。

  • 相关阅读:
    Google快照查看八大绝招[小摘]
    CSS li或dd 浮动后增加图片时多出4PX问题
    ASP.NET C# 邮件发送全解
    ff ie7 ie6 css支持问题[拼网页标记]
    net 中 等同与ajax的效果 ICallbackEventHandler
    让DIV层位于flash对象之上
    屏蔽JS错误代码
    META标签的使用摘录,网页跳转特效
    【转】海量数据处理常用思路和方法
    MFC画图定时刷新,能否做到完全无闪烁
  • 原文地址:https://www.cnblogs.com/linuxjava01/p/3729831.html
Copyright © 2011-2022 走看看