首页 > 代码库 > C#运用反射调用其他程序集中的代码

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.获取动态属性

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

 

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

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

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