• c#反射


     1  public static object loadDll(out bool isSuccess,string lpFileName, string Namespace, 
     2             string ClassName, string lpProcName, object[] ObjArray_Parameter)
     3         {
     4             isSuccess = false;
     5             try
     6             {
     7                 Assembly MyAssembly = Assembly.LoadFrom(lpFileName);
     8                 Type[] type = MyAssembly.GetTypes();
     9                 foreach (Type t in type)
    10                 {
    11                     if (t.Namespace == Namespace && t.Name == ClassName)
    12                     {
    13                         MethodInfo m = t.GetMethod(lpProcName);
    14                         if (m != null)
    15                         {
    16                             object o = Activator.CreateInstance(t);
    17                             object r=m.Invoke(o, ObjArray_Parameter);
    18                             isSuccess =true ;
    19                             return r;
    20                         }
    21                         else MessageBox.Show(" 装载出错 !");
    22                     }
    23                 }
    24             }
    25             catch (Exception e)
    26             {
    27                 MessageBox.Show(e.Message);
    28             }
    29             return (object)0;
    30         }

    这个方法可以动态的加载Dll,并且调用其中指定的方法,可以在不改变源程序的情况下,更换任意功能,甚至完全改变。

    比如说做一个游戏模块,可以将游戏做成dll,然后利用此方法在你的程序中任意动态的更换游戏。

  • 相关阅读:
    css中的属性
    css初识和css选择器
    前端html的简单认识
    数据库进阶了解
    数据库索引
    pymysql模块
    数据库的多表查询
    数据库中的行操作
    数据库和表操作以及完整性约束
    数据库概述
  • 原文地址:https://www.cnblogs.com/lipf/p/2529977.html
Copyright © 2020-2023  润新知