C# - 从.NET DLL(类库)执行代码而不引用它?

时间:2012-10-04 08:31:24

标签: c# class dll

我正在尝试找到一种方法,在我的应用程序启动后,我可以从.NET DLL执行代码。我在下面包含了一些伪代码,可能会尝试解释我正在尝试做什么。我知道它可能比我看起来要复杂得多。

ClassLibrary myLibrary = new ClassLibrary("C:\\Users\\Admin\\Desktop\\myTestLibrary.dll");
myLibrary.executeMethod("showMessageMethod", arg1, arg2, arg3...);

这就是我想要做的事情,虽然我知道它可能比那复杂得多!

我还要明确表示我明白你是想在你的项目中引用这个库...但是我的项目要求我不这样做方式。

提前致谢!

5 个答案:

答案 0 :(得分:12)

您需要从磁盘加载程序集,如下所示:

Assembly myLibrary = System.Reflection.Assembly
    .LoadFile("C:\\Users\\Admin\\Desktop\\myTestLibrary.dll");

之后,您需要使用反射获取正确的类型并调用正确的方法。当您要调用的类实现在启动时引用的程序集中定义的接口时,这将是最方便的:

Type myClass = (
    from type in myLibrary.GetExportedTypes()
    where typeof(IMyInterface).IsAssignableFrom(type)
    select type)
    .Single();

var instance = (IMyInterface)Activator.CreateInstance(myClass);

instance.executeMethod("showMessageMethod", arg1, arg2, arg3...);

答案 1 :(得分:3)

我希望以下代码示例有所帮助。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace zielonka.co.uk.stackoverflow.examples.Reflection
{
    class Program
    {
        static void Main(string[] args)
        {

            // dynamically load assembly from file Test.dll
            Assembly testAssembly = Assembly.LoadFile(@"c:\Test.dll");


            // get type of class Calculator from just loaded assembly
            Type calcType = testAssembly.GetType("Test.Calculator");


            // create instance of class Calculator
            object calcInstance = Activator.CreateInstance(calcType);

            // get info about property: public double Number
            PropertyInfo numberPropertyInfo = calcType.GetProperty("Number");

            // get value of property: public double Number
            double value = (double)numberPropertyInfo.GetValue(calcInstance, null);

            // set value of property: public double Number
            numberPropertyInfo.SetValue(calcInstance, 10.0, null);

            // get info about static property: public static double Pi
            PropertyInfo piPropertyInfo = calcType.GetProperty("Pi");

            // get value of static property: public static double Pi
            double piValue = (double)piPropertyInfo.GetValue(null, null);

            // invoke public instance method: public void Clear()
            calcType.InvokeMember("Clear",
                BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
                null, calcInstance, null);

            // invoke private instance method: private void DoClear()
            calcType.InvokeMember("DoClear",
                BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
                null, calcInstance, null);

            // invoke public instance method: public double Add(double number)
            double value = (double)calcType.InvokeMember("Add",
                BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
                null, calcInstance, new object[] { 20.0 });

            // invoke public static method: public static double GetPi()
            double piValue = (double)calcType.InvokeMember("GetPi",
                BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
                null, null, null);


            // get value of private field: private double _number
            double value = (double)calcType.InvokeMember("_number",
                BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,
                null, calcInstance, null);

        }
    }
}

答案 2 :(得分:1)

虽然您可以按史蒂文的答案详细说明加载装配,但还有另一种可能性。

您可以在解决方案中添加对程序集的引用,但不要将程序集部署为解决方案的一部分。这意味着您不必使用反射,而是使用AppDomain.AssemblyResolve事件。

您的需求尚不清楚,但实际上这是一种注入dll的方式。

例如,假设您已经为您支持的每个后端实现了数据库操作。

当您的代码尝试查找程序集时,它会在通常的位置查找,如果它尚未加载且无法找到,则会触发AssmemblyResolve事件,并在那里根据某些可访问的位置动态加载它一些配置选项。

可访问的位置,可以是网址,完全不同的路径,甚至是网络路径。显然不是这个例子,但它甚至可能是来自数据库的blob。

答案 3 :(得分:0)

首先解决方案是使用:

Assembly asm = Assembly.LoadFrom(path);

第二个是:MEF

答案 4 :(得分:0)

您正在寻找的可能是在运行时动态加载程序集的方法。

观看此视频:http://www.youtube.com/watch?v=WBoA36pwxJE

相关问题