在C#在运行时加载DLL [英] Loading DLLs at runtime in C#

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

问题描述

我试图找出你怎么能去在运行C#应用程序内导入和使用一个.dll。使用Assembly.LoadFile()我设法使我的程序加载DLL(这部分是绝对的工作,因为我能够得到与的toString类的名称()),但我不能使用'输出'从我的控制台应用程序中的方法。我编译.dll文件,然后将其移动到我的控制台的项目。有没有的CreateInstance之间的一个额外的步骤,然后能够使用的方法?

这是在我的DLL中的类:

 命名的DLL
{
    使用系统;    公共类的Class1
    {
        公共无效输出(字符串s)
        {
            Console.WriteLine(多个);
        }
    }
}

和这里是我想要加载DLL的应用程序

 命名空间的ConsoleApplication1
{
    使用系统;
    使用的System.Reflection;    类节目
    {
        静态无效的主要(字串[] args)
        {
            VAR DLL = Assembly.LoadFile(@C:\\的Visual Studio 2012 \\项目\\ ConsoleApplication1 \\ ConsoleApplication1 \\ DLL.dll);            的foreach(在DLL.GetExportedTypes类型类型())
            {
                变种C = Activator.CreateInstance(类型);
                c.Output(@你好);
            }            到Console.ReadLine();
        }
    }
}


解决方案

会员必须是在编译时解析直接从C#调用。否则,您必须使用反射或动态对象。

反射

 命名空间的ConsoleApplication1
{
    使用系统;
    使用的System.Reflection;    类节目
    {
        静态无效的主要(字串[] args)
        {
            VAR DLL = Assembly.LoadFile(@C:\\的Visual Studio 2012 \\项目\\ ConsoleApplication1 \\ ConsoleApplication1 \\ DLL.dll);            的foreach(在DLL.GetExportedTypes类型类型())
            {
                变种C = Activator.CreateInstance(类型);
                type.InvokeMember(输出,BindingFlags.InvokeMethod,空,C,新的对象[] {@你好});
            }            到Console.ReadLine();
        }
    }
}

动态(.NET 4.0)

 命名空间的ConsoleApplication1
{
    使用系统;
    使用的System.Reflection;    类节目
    {
        静态无效的主要(字串[] args)
        {
            VAR DLL = Assembly.LoadFile(@C:\\的Visual Studio 2012 \\项目\\ ConsoleApplication1 \\ ConsoleApplication1 \\ DLL.dll);            的foreach(在DLL.GetExportedTypes类型类型())
            {
                动态C = Activator.CreateInstance(类型);
                c.Output(@你好);
            }            到Console.ReadLine();
        }
    }
}

I am trying to figure out how you could go about importing and using a .dll at runtime inside a C# application. Using Assembly.LoadFile() I have managed to get my program to load the dll (this part is definitely working as I am able to get the name of the class with ToString()), however I am unable to use the 'Output' method from inside my console application. I am compiling the .dll then moving it into my console's project. Is there an extra step between CreateInstance and then being able to use the methods?

This is the class in my DLL:

namespace DLL
{
    using System;

    public class Class1
    {
        public void Output(string s)
        {
            Console.WriteLine(s);
        }
    }
}

and here is the application I want to load the DLL

namespace ConsoleApplication1
{
    using System;
    using System.Reflection;

    class Program
    {
        static void Main(string[] args)
        {
            var DLL = Assembly.LoadFile(@"C:\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\DLL.dll");

            foreach(Type type in DLL.GetExportedTypes())
            {
                var c = Activator.CreateInstance(type);
                c.Output(@"Hello");
            }

            Console.ReadLine();
        }
    }
}

解决方案

Members must be resolvable at compile time to be called directly from C#. Otherwise you must use reflection or dynamic objects.

Reflection

namespace ConsoleApplication1
{
    using System;
    using System.Reflection;

    class Program
    {
        static void Main(string[] args)
        {
            var DLL = Assembly.LoadFile(@"C:\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\DLL.dll");

            foreach(Type type in DLL.GetExportedTypes())
            {
                var c = Activator.CreateInstance(type);
                type.InvokeMember("Output", BindingFlags.InvokeMethod, null, c, new object[] {@"Hello"});
            }

            Console.ReadLine();
        }
    }
}

Dynamic (.NET 4.0)

namespace ConsoleApplication1
{
    using System;
    using System.Reflection;

    class Program
    {
        static void Main(string[] args)
        {
            var DLL = Assembly.LoadFile(@"C:\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\DLL.dll");

            foreach(Type type in DLL.GetExportedTypes())
            {
                dynamic c = Activator.CreateInstance(type);
                c.Output(@"Hello");
            }

            Console.ReadLine();
        }
    }
}

这篇关于在C#在运行时加载DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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