为什么Powershell没有看到它已经加载了这个组件?

时间:2012-06-08 07:35:03

标签: c# plugins powershell .net-assembly assembly-resolution

这是一个很长的,所以请紧紧抓住。

这个想法是,当引用Core.dll(见下文)时,应用程序将创建一个Core类型的新对象,并且在这样做时,构造函数将扫描C:\Problems文件夹对于包含使用IPlugin接口的类型的任何DLL。

当我从控制台应用程序引用Core.dll时,它可以正常工作。当我从PowerShell执行等效操作时,它不起作用,除非Core.dll在GAC中。

这些是详细信息:

我有一个程序集(Core.dll),它的结果是:

public class Core
{
    List<IPlugin> plugins = new List<IPlugin>();

    public Core(string path)
    {
        LoadPlugins(path);
        foreach (IPlugin plugin in plugins)
            plugin.Work();
    }

    void LoadPlugins(string path)
    {
        foreach (string file in Directory.GetFiles(path))
        {
            FileInfo fileInfo = new FileInfo(file);
            Assembly dllAssembly = Assembly.LoadFile(file);
            foreach (Type type in dllAssembly.GetTypes())
            {
                Type typeInterface = type.GetInterface("Problems.IPlugin");
                if (typeInterface != null)
                    plugins.Add((IPlugin)Activator.CreateInstance(
                                    dllAssembly.GetType(type.ToString())));
            }
        }
    }
}

public interface IPlugin
{
    string Name {get; set;}
    void Work();
}

然后,我有一个单独的程序集MyPlugin.dll,其中包含以下内容:

public class MyPlugin : IPlugin
{
    public string Name { get; set; }
    public void Work()
    {
        Console.WriteLine("Hello, World. I'm a plugin!");
    }
}

我使用第三个项目TestApp.exe(以Core.dll作为参考)测试了这一切:

static void Main(string[] args)
{
    Core core = new Core(@"C:\Problems");
    Console.ReadLine();
}

我将Core.dll和MyPlugin.dll都放入C:\ Problems中,而TestApp.exe就像一个魅力!所以,我写这个PowerShell脚本:

Add-Type -Path "C:\Problems\Core.dll"
$core = New-Object Problems.Core "C:\Problems"

这是我的结果(想象一堆红色):

New-Object : Exception calling ".ctor" with "1" argument(s): "Unable to load one or more o
f the requested types. Retrieve the LoaderExceptions property for more information."
At line:1 char:11
+ New-Object <<<<  Problems.Core "C:\Problems"
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

深入研究Exception,我发现:

PS C:\Problems> $Error[0].Exception.InnerException.LoaderExceptions
Could not load file or assembly 'Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

到目前为止,我的结论是当我将GetTypes()作为插件加载器的一部分运行时,它看到MyPlugin继承自Problems.IPlugin,它不知道在哪里可以找到。但是,我没有在Core.dll中加载它吗?

我可以解决这个问题的唯一方法是签署Core.dll程序集并将其添加到GAC,但这既烦人又不符合我的目的。

(所有类都在Problems命名空间中,我省略了大量的清理和过滤逻辑来说明问题。)

1 个答案:

答案 0 :(得分:2)

您可以通过查找正确的位置来确保处理程序集解决问题,即将Core类更改为以下内容来解决此问题:

public class Core
{
    List<IPlugin> plugins = new List<IPlugin>();
    string path;

public Core(string path)
{
    this.path = path;
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    LoadPlugins();
    foreach (IPlugin plugin in plugins)
        plugin.Work();
}

Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    var currentAssemblies = AppDomain.CurrentDomain.GetAssemblies();

    foreach (var assembly in currentAssemblies)
    {
        if (assembly.FullName == args.Name)
        {
            Console.WriteLine("resolved assembly " + args.Name + " using exising appdomain");
            return assembly;
        }
    }

    var file = args.Name.Split(new char[] { ',' })[0].Replace(@"\\", @"\");
    var dll = Path.Combine(path, file) + ".dll";
    Console.WriteLine("resolved assembly " + args.Name + " from " + path);
    return File.Exists(dll) ? Assembly.LoadFrom(dll) : null;
}

void LoadPlugins()
{
    foreach (string file in Directory.GetFiles(this.path, "*.dll"))
    {
        FileInfo fileInfo = new FileInfo(file);
        Assembly dllAssembly = Assembly.LoadFile(file);
        foreach (Type type in dllAssembly.GetTypes())
        {
            Type typeInterface = type.GetInterface("Problems.IPlugin");
            if (typeInterface != null)
                plugins.Add((IPlugin)Activator.CreateInstance(
                                dllAssembly.GetType(type.ToString())));
        }
    }
}

}