在外部程序中加载DLL?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的意思是您想按文件名动态加载它吗?那么是的,您可以使用
Assembly.LoadFile
方法如下:(翻译示例来自 这里,但我写的,所以不用担心!)
Do you mean you want to load it dynamically, by file name? Then yes, you can use the
Assembly.LoadFile
method as follows:(Translated example from here, but I wrote it so don't worry!)
只是以 minitech 的答案为基础。如果您可以使用 C# 4.0,则可以省略一些反射调用。
这里作为
dynamic
类型的instance
,您不必通过反射找到方法Calc
。但最好的方法是在上游定义一个接口
并在下游类中实现它
然后您可以最少地进行反射来构造对象。
这将为您带来最佳性能。
Just building on the answer by minitech.. If you can use C# 4.0 you can omit some reflection calls.
Here as
instance
of of typedynamic
, you don't have to find the methodCalc
by reflection.But the best way is to define a interface upstream
and implement it in your class downstream
Then you can do reflection minimally to construct the object.
This will give you the best performance.
右键单击 Visual Studio 项目资源管理器中的引用,然后选择程序集。然后你就可以使用它:
Right click on the References in the project explorer in Visual Studio and simply select the assembly. Then you can use it:
如果您使用 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