C#如何动态加载多个相关dll

osnot 2013-10-12 05:15:01
加载一个dll:

Assembly dllAmy = Assembly.LoadFile(dllPath);
Assembly.LoadFile(referDllPath);
classType = dllAmy.GetType(className);
method = classType.GetMethod(methodName);
classObj = dllAmy.CreateInstance(className);
string result = (string)method.Invoke(classObj, null);

但是现在这个method中会调用另外一个otherDll,我不知道如何加载进去?
请求帮助!
...全文
602 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
osnot 2013-10-23
  • 打赏
  • 举报
回复
引用 5 楼 caozhy 的回复:
可以啊,你全部Load进来再调用就可以。我就这么用过的。
引用 6 楼 sbwwkmyd 的回复:
试试Assembly.LoadFrom
引用 7 楼 wanghui0380 的回复:
Assembly.LoadFrom可以自动加载依赖项
先谢谢三位回复,我测试的代码如下: 1、DynmicLoadDLLReferLib项目下:NumberTool.cs有方法GetNumber():

public class NumberTool
    {
        public string GetNumber(int number)
        {
            return string.Format("GetNumber:{0}", number);
        }
    }
2、DynmicLoadDLLLib项目中TimeTool.cs调用NumberTool.GetNumber()方法:

public class TimeTool
    {
        public string GetNowTime()
        {
            return string.Format("GetNowTime():{0}, Invoke DynmicLoadDLLReferLib.NumberTool.GetNumber({1}):{2}", 
                DateTime.Now.ToString("HH:mm:ss:FFF"),
                1, new NumberTool().GetNumber(1));
        }
    }
3、我的测试代码:

public static void LoadDLL()
        {
            string dllPath = @"E:\DynmicLoadDLLLib\bin\Debug\DynmicLoadDLLLib.dll";
            string referDllPath = @"E:\DynmicLoadDLLReferLib\bin\Debug\DynmicLoadDLLReferLib.dll";
            string className = "DynmicLoadDLLLib.TimeTool"; //类的完全限定名称:必须使用名称空间+类名称  
            string methodName = "GetNowTime"; //方法的名称  
            string parameClassName = "";
            Type classType;
            MethodInfo method;
            Object classObj;
            Assembly dllAmy;
            string result;

            try
            {
		//第一种方法LoadFile,把需要的全部load出来
                //AppDomain domain = AppDomain.CreateDomain("_tempAppDomain");
                dllAmy = Assembly.LoadFile(dllPath);
                Assembly.LoadFile(referDllPath);
                classType = dllAmy.GetType(className);
                method = classType.GetMethod(methodName);
                classObj = dllAmy.CreateInstance(className);//必须使用名称空间+类名称  
                result = (string)method.Invoke(classObj, null); //实例方法的调用  
            }
            catch (Exception ex)
            {
                //throw ex;
            }

	    //第二种方法LoadFrom
            dllAmy = Assembly.LoadFrom(dllPath);
            //Assembly.LoadFile(referDllPath);
            classType = dllAmy.GetType(className);
            method = classType.GetMethod(methodName);
            classObj = dllAmy.CreateInstance(className);//必须使用名称空间+类名称  
            result = (string)method.Invoke(classObj, null); //实例方法的调用  
        }
4、结果 上面两个操作均报异常:未能加载文件或程序集“DynmicLoadDLLReferLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统找不到指定的文件。 5、结论 两种方法都不靠谱。。。
wanghui0380 2013-10-14
  • 打赏
  • 举报
回复
Assembly.LoadFrom可以自动加载依赖项
showjim 2013-10-14
  • 打赏
  • 举报
回复
试试Assembly.LoadFrom
threenewbee 2013-10-14
  • 打赏
  • 举报
回复
可以啊,你全部Load进来再调用就可以。我就这么用过的。
osnot 2013-10-14
  • 打赏
  • 举报
回复
引用 3 楼 lizhi3186575 的回复:
使用AggregateCatalog类,具体参考下帖子:http://social.msdn.microsoft.com/Forums/zh-CN/b26b35ae-ca68-482e-afc9-de2bc10f04e4/mef-directorycatalog?forum=visualcshartzhchs
首先,谢谢,你发的链接我看过了,感觉MEF是做插件的,和我这个需求不太符合,而且我也不会使用MEF,我觉得应该有个方法可以加载多个DLL。
osnot 2013-10-12
  • 打赏
  • 举报
回复
引用 1 楼 feiyun0112 的回复:
看这个行不行 动态调用DLL时不能加载依赖的程序集 http://www.cnblogs.com/feiyun0112/archive/2009/07/03/1516512.html ***************************************************************************** http://feiyun0112.cnblogs.com/
谢谢,有没有一开始就可以加载多个dll的方法?
feiyun0112 2013-10-12
  • 打赏
  • 举报
回复
看这个行不行
动态调用DLL时不能加载依赖的程序集
http://www.cnblogs.com/feiyun0112/archive/2009/07/03/1516512.html

*****************************************************************************
http://feiyun0112.cnblogs.com/

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧