Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change .dll in runtime

Tags:

c#

.net

dll

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;
        }
    }
}
like image 599
Nail Shakirov Avatar asked Oct 01 '13 12:10

Nail Shakirov


People also ask

How do I change the DLL code in Visual Studio?

You can easily open a DLL file in Visual Studio the same way you would any other file. To do so, click File in the menu bar at the top, followed by Open. Then click File and select the DLL file you want to open and click Open. This will open the DLL file in a new Resource Editor window.

Can you make a DLL with C#?

This tutorial shows, how to build a simple Class Library . DLL in the C# Programming Language. The Class Library . DLL contains program code, data, and resources that can be can used by other programs and are easily implemented into other Visual Studio projects.


2 Answers

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...)

like image 74
MichaC Avatar answered Oct 05 '22 22:10

MichaC


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.

like image 40
Lloyd Avatar answered Oct 06 '22 00:10

Lloyd