Change .dll in runtime

2019-06-28 05:46发布

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. 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? Here's my example, it takes my first dll, but second time it doesn't. First dll - sum, second - deducted.

    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:

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

标签: c# .net dll
2条回答
We Are One
2楼-- · 2019-06-28 06:09

Assemblies are generally loaded into an AppDomain once and you cannot unload them once loaded.

You can create a new AppDomain and load your assemblies into this and when you release this the assemblies will be unloaded. 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.

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://adrianvintu.com/blogengine/post/Unloadable-plugins.aspx

Hopefully these will aid you.

查看更多
Explosion°爆炸
3楼-- · 2019-06-28 06:23

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 allows you to use any assembly dynamically and even drop dlls into a folder and build a MEF catalog out of it.

Actually Visual Studio and uses MEF internally for extensiblility (Plugins...)

查看更多
登录 后发表回答