我使用 AppDomain.CurrentDomain.Load 从外部载入2个dll文件
可以在AppDomain.CurrentDomain.GetAssemblies();中看到已经程序集已经载入了
但是我用AppDomain.CurrentDomain.CreateInstance 创建的时候又说找不到程序集 Assembly[] Ass_s = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly a in Ass_s)
{
Response.Write(a.FullName + "<br />");
foreach (Type t in a.GetTypes())
{
Response.Write("___" + t.ToString() + "<br />");
if (t.Name == "testClass" || t.Name == "test2Class")
{
object o = a.CreateInstance(t.ToString());
Response.Write(o);
}
}
}
如果我这样创建 的话 testClass就无法创建 因为他又依赖了test2Class System.Reflection.Assembly o1Ass = AppDomain.CurrentDomain.Load(loadFile("/testDll.dll"), loadFile("/testDll.pdb"));
System.Reflection.Assembly o2Ass = AppDomain.CurrentDomain.Load(loadFile("/test2Dll.dll"), loadFile("/test2Dll.pdb"));
//这个会说找不到程序集
object obj1 = AppDomain.CurrentDomain.CreateInstance("test2Dll", "test2Dll.test2Class");
//同上
object obj2 = AppDomain.CurrentDomain.CreateInstance(o2Ass.FullName, "test2Dll.test2Class");//创建成功
object o2 = o2Ass.CreateInstance("test2Dll.test2Class");
//返回null
object o1 = o2Ass.CreateInstance("testDll.testClass");
namespace test2Dll
{
public class test2Class
{
public string OXN()
{
return "hahahao";
}
}
}
namespace testDll
{
public class testClass
{
public string str {
get { return new test2Dll.test2Class().OXN(); }
}
}
}
可以在AppDomain.CurrentDomain.GetAssemblies();中看到已经程序集已经载入了
但是我用AppDomain.CurrentDomain.CreateInstance 创建的时候又说找不到程序集 Assembly[] Ass_s = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly a in Ass_s)
{
Response.Write(a.FullName + "<br />");
foreach (Type t in a.GetTypes())
{
Response.Write("___" + t.ToString() + "<br />");
if (t.Name == "testClass" || t.Name == "test2Class")
{
object o = a.CreateInstance(t.ToString());
Response.Write(o);
}
}
}
如果我这样创建 的话 testClass就无法创建 因为他又依赖了test2Class System.Reflection.Assembly o1Ass = AppDomain.CurrentDomain.Load(loadFile("/testDll.dll"), loadFile("/testDll.pdb"));
System.Reflection.Assembly o2Ass = AppDomain.CurrentDomain.Load(loadFile("/test2Dll.dll"), loadFile("/test2Dll.pdb"));
//这个会说找不到程序集
object obj1 = AppDomain.CurrentDomain.CreateInstance("test2Dll", "test2Dll.test2Class");
//同上
object obj2 = AppDomain.CurrentDomain.CreateInstance(o2Ass.FullName, "test2Dll.test2Class");//创建成功
object o2 = o2Ass.CreateInstance("test2Dll.test2Class");
//返回null
object o1 = o2Ass.CreateInstance("testDll.testClass");
namespace test2Dll
{
public class test2Class
{
public string OXN()
{
return "hahahao";
}
}
}
namespace testDll
{
public class testClass
{
public string str {
get { return new test2Dll.test2Class().OXN(); }
}
}
}
#region Win API 声明
class LoadDllAPI
{
[DllImport("kernel32.dll")]
public extern static IntPtr LoadLibrary(string path);
[DllImport("kernel32.dll")]
public extern static IntPtr GetProcAddress(IntPtr lib, string funcName);
[DllImport("kernel32.dll")]
public extern static bool FreeLibrary(IntPtr lib);
[DllImport("kernel32.dll")]
public static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("user32", EntryPoint = "CallWindowProc")]
public static extern int CallWindowProc(IntPtr lpPrevWndFunc, int hwnd, int MSG, int wParam, int lParam);
}
#endregion
public class LoadDll
{
IntPtr DllLib;//DLL文件名柄
#region 构造函数
public LoadDll()
{ }
public LoadDll(string dllpath)
{
DllLib = LoadDllAPI.LoadLibrary(dllpath);
}
#endregion
/// <summary>
/// 析构函数
/// </summary>
~LoadDll()
{
LoadDllAPI.FreeLibrary(DllLib);//释放名柄
}
public void initPath(string dllpath)
{
if (DllLib == IntPtr.Zero)
{
DllLib = LoadDllAPI.LoadLibrary(dllpath);
}
}
/// <summary>
/// 获取DLL中一个方法的委托
/// </summary>
/// <param name="methodname"></param>
/// <param name="methodtype"></param>
/// <returns></returns>
public Delegate InvokeMethod(string methodname, Type methodtype)
{
IntPtr MethodPtr = LoadDllAPI.GetProcAddress(DllLib, methodname);
return (Delegate)Marshal.GetDelegateForFunctionPointer(MethodPtr, methodtype);
}
}
#region Win API 声明
class LoadDllAPI
{
[DllImport("kernel32.dll")]
public extern static IntPtr LoadLibrary(string path);
[DllImport("kernel32.dll")]
public extern static IntPtr GetProcAddress(IntPtr lib, string funcName);
[DllImport("kernel32.dll")]
public extern static bool FreeLibrary(IntPtr lib);
[DllImport("kernel32.dll")]
public static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("user32", EntryPoint = "CallWindowProc")]
public static extern int CallWindowProc(IntPtr lpPrevWndFunc, int hwnd, int MSG, int wParam, int lParam);
}
#endregion
public class LoadDll
{
IntPtr DllLib;//DLL文件名柄
#region 构造函数
public LoadDll()
{ }
public LoadDll(string dllpath)
{
DllLib = LoadDllAPI.LoadLibrary(dllpath);
}
#endregion
/// <summary>
/// 析构函数
/// </summary>
~LoadDll()
{
LoadDllAPI.FreeLibrary(DllLib);//释放名柄
}
public void initPath(string dllpath)
{
if (DllLib == IntPtr.Zero)
{
DllLib = LoadDllAPI.LoadLibrary(dllpath);
}
}
/// <summary>
/// 获取DLL中一个方法的委托
/// </summary>
/// <param name="methodname"></param>
/// <param name="methodtype"></param>
/// <returns></returns>
public Delegate InvokeMethod(string methodname, Type methodtype)
{
IntPtr MethodPtr = LoadDllAPI.GetProcAddress(DllLib, methodname);
return (Delegate)Marshal.GetDelegateForFunctionPointer(MethodPtr, methodtype);
}
}
以上方法编译.调用:view plaincopy to clipboardprint?
* loadDLL.LoadDll loaddll = new loadDLL.LoadDll();//实例化加载DLL文件的类,,如上
* public delegate int delegateadd(IntPtr a,IntPtr b);//声明此方法的一个委托
//一个调用用的按钮
private void button1_Click(object sender, EventArgs e)
{
loaddll.initPath("testdll3.dll");//载入文件
delegateadd m = (delegateadd)loaddll.InvokeMethod("add", typeof(add));//获取其中方法的委托
int a=1;int b=2;
int re = m(out a, out b);//得到RE=3,,成功
} 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/adsdassadfasdfasdf/archive/2010/08/12/5805988.aspx