更改.dll文件在运行时 [英] Change .dll in runtime

查看:154
本文介绍了更改.dll文件在运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个庞大的应用程序,其中我的解决方案中的一个项目,使报告。 我要添加新的报告(更新报告)没有建立我的项目,只需添加 .DLL 文件。我读组装的AppDomain ,但我不知道是不是真的增加新的DLL的新报告,以及如何更新运行旧报告好方法吗? 下面是我的榜样,它需要我的第一个DLL,但第二次没有。首先DLL - 总之,第二次 - 扣除

 静态无效的主要(字串[] args)
    {
        尝试
        {
            //第一个域
            AppDomain中域= AppDomain.CreateDomain(MYDOMAIN);
            AssemblyDll ASB1 =新AssemblyDll();
            Console.WriteLine(asb1.AssemblyMethod(1));
            AppDomain.Unload(域);
            Console.ReadKey();

            //第二个域
            AppDomain中NEWDOMAIN = AppDomain.CreateDomain(myNewDomain);
            AssemblyDll ASB2 =新AssemblyDll();
            Console.WriteLine(asb2.AssemblyMethod(2));
            AppDomain.Unload(NEWDOMAIN);
            Console.ReadKey();
        }
        赶上(例外前)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

公共类AssemblyDll
{
    公共字符串AssemblyMethod(INT版)
    {
        //加载的.dll
        装配装配= Assembly.LoadFrom(@../../../../大会/ DynamicDLL+版本+.DLL);
        类型类型= assembly.GetType(DynamicDLL.Dynamic);
        对象实例= Activator.CreateInstance(类型);
        MethodInfo的[]方法= type.GetMethods();
        //调用方法
        对象result =方法[0] .Invoke(例如,新的对象[] {5,3});
        返回result.ToString();
    }
}
 

我的.dll文件来自:

 命名空间DynamicDLL
{
    公共类动态
    {
        公众诠释DynamicMethod的(INT A,INT B)
        {
            返回A + B;
            //返回 -  B;
        }
    }
}
 

解决方案

如果你想写类似的插件和类似插件的方法,你应该看一看MEF的 http://msdn.microsoft.com/en/library/vstudio/dd460648.aspx

MEF允许你动态地使用任何组件,甚至下降dll文件到一个文件夹,并建立一个MEF目录出来。

其实Visual Studio和使用MEF内部的extensiblility(插件...)

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;
        }
    }
}

解决方案

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

这篇关于更改.dll文件在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆