从特定文件夹动态加载DLL? [英] Dynamically load a DLL from a specific folder?

查看:163
本文介绍了从特定文件夹动态加载DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有这个代码:

var shellViewLibrary = Assembly.LoadFrom(Path.Combine(_DllsPath, _DllShellView));
IEnumerable<Type> types = shellViewLibrary.GetTypes();

foreach (Type type in types)
{
    var typeIShellViewInterface = type.GetInterface(_NamespaceIShellView, false);
    if (typeIShellViewInterface != null)
    {
        //here
    }
}

事情是,我得到 //这里我想使用 Activator.CreateInstance 创建一个对象,其类型是在特定文件夹中键入 (构建文件夹外)
I尝试了大约20种不同的东西,其中大部分是这样的: http://msdn.microsoft .com / en-us / library / d133hta4.aspx
但无工作...
我尝试的典型的是:

The thing is that where I got //here I want to use Activator.CreateInstance to create an object whose type is type in a specific folder (that is outside the build folder) I tried about 20 different things, most of them with this : http://msdn.microsoft.com/en-us/library/d133hta4.aspx but none works... The typical thing I tried is :

object MyObj = Activator.CreateInstance(shellViewLibrary.FullName, type.FullName);

object MyObj = Activator.CreateInstance(Path.Combine(_DllsPath, _DllShellView), type.FullName);

我总是有不同的例外,最常见的是:

I always got different exception, the most common being :

XamlParseException

我觉得我' m没有使用Activator.CreateInstance以正确的方式与2个参数。我应该怎么做?

I feel like that I'm not using Activator.CreateInstance in the right way with 2 parameters. What should I do ?

推荐答案

一旦你打电话给这条线

var shellViewLibrary = Assembly.LoadFrom(Path.Combine(_DllsPath, _DllShellView)); 

程序集已加载到内存中。只要您从中指定类型,就可以使用Activator.CreateInstance创建类型。即:不需要进一步指定其中类型。

The assembly has been loaded in to memory. So long as you specify types correctly from this then you will be able to use Activator.CreateInstance to create the types. ie: It is not necessary to further specify where the type is.

关于Activator,请从 MSDN CreateInstance方法可以接受一个System.Type。我只是在你的if语句中使用这个方法:

Regarding Activator, from MSDN the CreateInstance method can accept a System.Type. I would just use this method inside your if-statement:

Activator.CreateInstance(Type type);

我将尝试调试此操作首先创建类型,然后将其传递给CreateInstance 。您可能会发现类型创建本身失败(由于未解析的程序集)或该类型的实例化(由于构造函数中的异常)。乍看之下,您的代码似乎是正确的:

What I would try to do to debug this is first create the type and then pass it in to CreateInstance. You may find that the Type creation itself is failing (due to unresolved assembly) or the instantiation of that type (due to exception in the constructor). At first glance your code here appears to be correct:

foreach (Type type in types)      
{          
    var typeIShellViewInterface = type.GetInterface(_NamespaceIShellView, false);          
    if (typeIShellViewInterface != null)          
    {              
        try
        {
            // I assume you are calling this line at the point marked 'here'. 
            // To debug the creation wrap in a try-catch and view the inner exceptions
            var result = Activator.CreateInstance(type);          
        }
        catch(Exception caught)
        {
            // When you hit this line, look at caught inner exceptions
            // I suspect you have a broken Xaml file inside WPF usercontrol
            // or Xaml resource dictionary used by type
            Debugger.Break();
        }
    }      
}  

在您的问题中,您指定你得到一个 XamlParseException 。这听起来像我所讨论的类型是一个UserControl(或以其他方式指向WPF Xaml资源文件),并且该Xaml文件中有一个错误,即与您使用Assembly.Load或Activator.CreateInstance无关。

In your question you specify that you are getting a XamlParseException. It sounds to me like the type in question is a UserControl (or otherwise refers to a WPF Xaml resource file) and there is an error in that Xaml file, i.e. nothing to do with your usage of Assembly.Load or Activator.CreateInstance.

你可以尝试发布内部异常来更好地了解问题是什么吗?

Could you try posting the inner exception(s) to get a better idea on what the problem is?

这篇关于从特定文件夹动态加载DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆