在外部程序中加载DLL?

发布于 2024-12-27 21:04:23 字数 254 浏览 0 评论 0原文

我有一个 C# ClassLibrary,其中包含一个对两个数字求和的函数:

namespace ClassLibrary1
{
    public class Calculator
    {
        public int Calc(int i, int b) {
            return i + b;
        }
    }
}

我想从外部的其他 C# 应用程序加载此 dll。我该怎么做?

I have a C# ClassLibrary that contains a function to sum two numbers:

namespace ClassLibrary1
{
    public class Calculator
    {
        public int Calc(int i, int b) {
            return i + b;
        }
    }
}

I want to load this dll from other C# application externally. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南烟 2025-01-03 21:04:23

您的意思是您想按文件名动态加载它吗?那么是的,您可以使用 Assembly.LoadFile 方法如下:(

// Load the assembly
Assembly a = Assembly.LoadFile(@"C:\Path\To\Your\DLL.dll");

// Load the type and create an instance
Type t = a.GetType("ClassLibrary1.Calculator");
object instance = a.CreateInstance("ClassLibrary1.Calculator");

// Call the method
MethodInfo m = t.GetMethod("Calc");
m.Invoke(instance, new object[] {}); // Get the result here

翻译示例来自 这里,但我写的,所以不用担心!)

Do you mean you want to load it dynamically, by file name? Then yes, you can use the Assembly.LoadFile method as follows:

// Load the assembly
Assembly a = Assembly.LoadFile(@"C:\Path\To\Your\DLL.dll");

// Load the type and create an instance
Type t = a.GetType("ClassLibrary1.Calculator");
object instance = a.CreateInstance("ClassLibrary1.Calculator");

// Call the method
MethodInfo m = t.GetMethod("Calc");
m.Invoke(instance, new object[] {}); // Get the result here

(Translated example from here, but I wrote it so don't worry!)

未蓝澄海的烟 2025-01-03 21:04:23

只是以 minitech 的答案为基础。如果您可以使用 C# 4.0,则可以省略一些反射调用。

    public static void Main()
    {  
       Assembly ass = Assembly.LoadFile(@"PathToLibrar\ClassLibraryTest.dll");
       var type = ass.GetType("ClassLibrary1.Calculator");
       dynamic instance = Activator.CreateInstance(type);
       int add = instance.Calc(1, 3);
    }

这里作为dynamic类型的instance,您不必通过反射找到方法Calc

但最好的方法是在上游定义一个接口

 public interface ICalculator
    {
        int Calc(int i, int b);
    }

并在下游类中实现它

public class Calculator : ICalculator
{
    public int Calc(int i, int b)
    {
        return i + b;
    }
}

然后您可以最少地进行反射来构造对象。

    public static void Main()
    {  
       Assembly ass = Assembly.LoadFile(@"PathToLibrar\ClassLibraryTest.dll");
       var type = ass.GetType("ClassLibrary1.Calculator");
       ICalculator instance = Activator.CreateInstance(type) as ICalculator;
       int add = instance.Calc(1, 3);
    }

这将为您带来最佳性能。

Just building on the answer by minitech.. If you can use C# 4.0 you can omit some reflection calls.

    public static void Main()
    {  
       Assembly ass = Assembly.LoadFile(@"PathToLibrar\ClassLibraryTest.dll");
       var type = ass.GetType("ClassLibrary1.Calculator");
       dynamic instance = Activator.CreateInstance(type);
       int add = instance.Calc(1, 3);
    }

Here as instance of of type dynamic, you don't have to find the method Calc by reflection.

But the best way is to define a interface upstream

 public interface ICalculator
    {
        int Calc(int i, int b);
    }

and implement it in your class downstream

public class Calculator : ICalculator
{
    public int Calc(int i, int b)
    {
        return i + b;
    }
}

Then you can do reflection minimally to construct the object.

    public static void Main()
    {  
       Assembly ass = Assembly.LoadFile(@"PathToLibrar\ClassLibraryTest.dll");
       var type = ass.GetType("ClassLibrary1.Calculator");
       ICalculator instance = Activator.CreateInstance(type) as ICalculator;
       int add = instance.Calc(1, 3);
    }

This will give you the best performance.

寒尘 2025-01-03 21:04:23

右键单击 Visual Studio 项目资源管理器中的引用,然后选择程序集。然后你就可以使用它:

using ClassLibrary1;

class Program
{
    static void Main()
    {
        Calculator calc = new Calculator();
        int result = calc.Cal(1, 2);
    }
}

Right click on the References in the project explorer in Visual Studio and simply select the assembly. Then you can use it:

using ClassLibrary1;

class Program
{
    static void Main()
    {
        Calculator calc = new Calculator();
        int result = calc.Cal(1, 2);
    }
}
玩套路吗 2025-01-03 21:04:23

如果您使用 Visual Studio,您可以在项目中引用此 dll,然后在新源代码中包含命名空间

In case your'e using visual studio you can reference this dll in your project and than include the namespace in your new source code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文