如何在c#运行时将参数传递给外部dll [英] How to pass parameters to an external dll during runtime in c#
问题描述
大家好。
我有这个名为dllSource.dll的dll,它包含:
Hi everyone.
I have this dll named dllSource.dll, and it contains:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace dllSource
{
public class Operation
{
public int Add(int x, int y)
{
int z;
if ((x == 10) && (y == 20))
{
z = x + y;
}
else
{
z = x;
}
return z;
}
}
}
我打算做的是打电话给那个dll并通过它上面的参数。
我希望将5传递给x,将10传递给y。
我将如何这样做?
另外,如果我想得到dll的参数类型,我该怎么做?
谢谢
What I intend to do is to call that dll and pass parameters on it.
I want to pass 5 to x and 10 to y.
How will I so that?
Also, if I want to get the parameter type of the dll, how will I do it?
Thanks
推荐答案
您需要将dllSource.dll添加到其他项目的References
中。
在该项目的代码中包含一行使用dllSource;
现在你可以在你的dll中使用这个类了像任何其他类或类型一样。例如
You will need to add your dllSource.dll to theReferences
of your other project.
In the code for that project include a lineusing dllSource;
Now you can use the class in your dll like any other class or type. E.g.
private void button1_Click(object sender, EventArgs e)
{
Operation o = new Operation();
int i = o.Add(5, 10);
// ... etc
}
获取参数类型和返回类型...
A)手动 - 右键单击解决方案资源管理器参考部分中的dllSource,然后选择在对象浏览器中查看
B)键入代码 - 使用上面的代码作为示例键入 o。
并允许显示智能感知,然后将鼠标悬停在您想要信息的功能上。
C)以编程方式 - 使用反思 - 看看这个文章 [ ^ ]
由以下评论提示...你需要将类和函数声明为 public
以上任何一种工作
To get the parameter type(s) and return type ...
A) Manually - Right-click on the dllSource in the References section of Solution Explorer and select "View in Object Browser"
B) Whilst typing code - using my code above as an example type o.
and allow the intellisense to display then hover your mouse over the function you want the information on.
C) Programmatically - Use Reflection - have a look at this article[^]
prompted by comment below ... you will need to declare the class and function as public
for any of the above to work
这篇关于如何在c#运行时将参数传递给外部dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!