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,然后利用此方法在你的程序中任意动态的更换游戏。

posted on 2012-06-01 10:13  EvilGenius  阅读(196)  评论(0编辑  收藏  举报