简体   繁体   English

在运行时更改.dll

[英]Change .dll in runtime

I have a huge application where one project of my solution makes reports. 我有一个庞大的应用程序,我的解决方案的一个项目报告。 I want to add new report (update report) without building my project, just add .dll files. 我想在不构建项目的情况下添加新报告(更新报告),只需添加.dll文件。 I read about Assembly and AppDomain , but I don't know is it really good way to add new dll for new report and how to update old report in runtime? 我读到了关于AssemblyAppDomain ,但我不知道为新报告添加新的dll以及如何在运行时更新旧报告真的很好吗? Here's my example, it takes my first dll, but second time it doesn't. 这是我的例子,它需要我的第一个dll,但第二次没有。 First dll - sum, second - deducted. 第一个dll - 总和,秒 - 扣除。

    static void Main(string[] args)
    {
        try
        {
            //first domain
            AppDomain domain = AppDomain.CreateDomain("MyDomain");
            AssemblyDll asb1 = new AssemblyDll();
            Console.WriteLine(asb1.AssemblyMethod(1));
            AppDomain.Unload(domain);
            Console.ReadKey();

            //second domain
            AppDomain newDomain = AppDomain.CreateDomain("myNewDomain");
            AssemblyDll asb2 = new AssemblyDll();
            Console.WriteLine(asb2.AssemblyMethod(2));
            AppDomain.Unload(newDomain);
            Console.ReadKey();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

public class AssemblyDll
{
    public string AssemblyMethod(int version)
    {
        //loading .dll
        Assembly assembly = Assembly.LoadFrom(@"../../../../Assembly/DynamicDLL" + version + ".dll");
        Type type = assembly.GetType("DynamicDLL.Dynamic");
        object instance = Activator.CreateInstance(type);
        MethodInfo[] methods = type.GetMethods();
        //invoke method
        object result = methods[0].Invoke(instance, new object[] { 5, 3 });
        return result.ToString();
    }
}

My .dll file comes from: 我的.dll文件来自:

namespace DynamicDLL
{
    public class Dynamic
    {
        public int DynamicMethod(int a, int b)
        {
            return a + b;
            //return a - b;
        }
    }
}

Assemblies are generally loaded into an AppDomain once and you cannot unload them once loaded. 程序集通常一次加载到AppDomain ,一旦加载就无法卸载它们。

You can create a new AppDomain and load your assemblies into this and when you release this the assemblies will be unloaded. 您可以创建一个新的AppDomain并将程序集加载到此中,当您释放它时,将卸载程序集。 However the caveat here is you cannot directly communicate between two AppDomain you have to marshal between the two using some other method like remoting. 但是这里需要注意的是,你不能直接在两个AppDomain之间进行通信,你必须使用其他一些方法(例如远程处理)来编组。

There's been much wrote on this in terms of plugins and making plugins unloadable, a quick Google search presented these: 在插件和插件无法加载方面有很多关于这方面的文章,快速谷歌搜索提出了这些:

http://www.brad-smith.info/blog/archives/500 http://www.brad-smith.info/blog/archives/500

http://adrianvintu.com/blogengine/post/Unloadable-plugins.aspx http://adrianvintu.com/blogengine/post/Unloadable-plugins.aspx

Hopefully these will aid you. 希望这些能帮到你。

If you want to write something like plugins and like the plugin approach, you should take a look at MEF http://msdn.microsoft.com/en/library/vstudio/dd460648.aspx 如果你想写插件之类的东西,比如插件方法,你应该看看MEF http://msdn.microsoft.com/en/library/vstudio/dd460648.aspx

MEF allows you to use any assembly dynamically and even drop dlls into a folder and build a MEF catalog out of it. MEF允许您动态使用任何程序集,甚至将dll删除到文件夹中,并从中构建MEF目录。

Actually Visual Studio and uses MEF internally for extensiblility (Plugins...) 实际上是Visual Studio并在内部使用MEF来实现可扩展性(插件......)

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

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