简体   繁体   English

在动态编译的代码中引用动态加载的程序集

[英]Referencing dynamically loaded assemblies in dynamically compiled code

I have a situation here in a .net project in which I'm compiling C# scripts into in-memory assemblies using CodeDomProvider.我在 .net 项目中遇到这种情况,我正在使用 CodeDomProvider 将 C# 脚本编译成内存程序集。

In the CompilerParameters class that I use for compiling, I reference some assemblies by title (System.dll and one that is part of the project) and everything works so far.在我用于编译的 CompilerParameters class 中,我按标题引用了一些程序集(System.dll 和项目的一部分),到目前为止一切正常。 However some scripts are using some code (through an interface and polymorphism) that is found in an assembly that is precompiled but loaded dynamically from disk (like a plug-in).然而,一些脚本正在使用一些代码(通过接口和多态性),这些代码在预编译但从磁盘动态加载的程序集中(如插件)中找到。 In this situation it won't work and the issue is that I don't know how to reference the dynamically loaded assembly when compiling the script dynamically.在这种情况下它不起作用,问题是我不知道在动态编译脚本时如何引用动态加载的程序集。

If I put my script in a class library and from that class library I reference the plug-in assembly, it all works fine, but if I add the name of the plug-in assembly (the dll file) to the referenced assemblies in CompilerParameters and compile the script dynamically afterwards, the script is executed but the code that is located in the plug-in dll is not.如果我将我的脚本放在 class 库中,并从该 class 库中引用插件程序集,一切正常,但如果我将插件程序集的名称(Compiler 程序集中的 Z06416233FE5EC4C5933122E4AB248AF1 中的 Z06416233FE5EC4C5933122E4AB248AF1 文件)然后动态编译脚本,脚本被执行,但位于插件 dll 中的代码没有。

I hope it is clear what I am trying to do here, please help if you know anything about this because I'm struggling for some time now and nothing that I have tried works.我希望很清楚我在这里要做什么,如果您对此有所了解,请提供帮助,因为我现在已经挣扎了一段时间,而且我尝试过的任何方法都不起作用。

Thanks!谢谢!

Edit: Here is the code for loading the plug-in dlls:编辑:这是加载插件 dll 的代码:

        DirectoryInfo di = new DirectoryInfo(@".\Plugins");
        FileInfo[] files = di.GetFiles("*.dll");
        foreach (FileInfo fi in files)
        {
            try
            {
                //load all dll files from the app pack directory
                Assembly asm = Assembly.LoadFrom(fi.FullName);
                Assemblies.Add(asm);
                foreach (Type type in asm.GetTypes())
                {
                    try
                    {
                        object instance = null;
                        instance = Activator.CreateInstance(type);

After this I store the instance in a dictionary for further usage.在此之后,我将实例存储在字典中以供进一步使用。 On the other side, Here is how I try to pass the references to the script that will be compiled:另一方面,这是我尝试将引用传递给将要编译的脚本的方法:

        CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
        List<string> referenceAssemblies = new List<string>();
        referenceAssemblies.Add("System.dll");
        referenceAssemblies.Add("VFS.dll");
        foreach (Assembly asm in PluginManager.Instance.Assemblies)
        {
            referenceAssemblies.Add(asm.Location);
        }
        CompilerParameters compilerParameters = new CompilerParameters(referenceAssemblies.ToArray());

This works if I don't use any class that is part of a plug-in.如果我不使用作为插件一部分的任何 class ,则此方法有效。

I have discovered the problem and it is in a different area, this part works fine.我发现了问题,它在不同的区域,这部分工作正常。

@Insipid - Thanks for your inspiring answer, because nothing was actually happening, and that was what made me look into another place and see the issue. @Insipid - 感谢您鼓舞人心的回答,因为实际上没有发生任何事情,这就是让我寻找另一个地方并看到问题的原因。

The code I pasted above works just fine and can be used if anyone would want to implement such mechanisms in the future.我在上面粘贴的代码工作得很好,如果将来有人想实现这样的机制,可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM