这是一个很长的时间,请紧紧捕获。

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

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

这些是详细信息:

我有一个程序集(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

深入研究异常,我发现:
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,但这既烦人又不符合我的目的。

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

最佳答案

您可以通过确保在正确的位置进行处理来解决程序集解决问题,例如将您的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())));
        }
    }
}

}

关于c# - Powershell为什么看不到它已经加载了该程序集?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10944771/

10-13 07:46
查看更多