运行时调用方法

发布于 2024-08-19 23:12:54 字数 75 浏览 2 评论 0原文

我想知道是否可以在运行时加载 .net DLL、查看可用的方法并在运行时执行一个。

如果可能的话你能给我指出正确的方向吗

I am wondering if it is possible to load a .net DLL at runtime, view the methods available and execute one at runtime.

If this is possible could you point me in the right direction

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

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

发布评论

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

评论(4

滿滿的愛 2024-08-26 23:12:54

通常,您使用 System.Reflection 类来完成此任务。

具体来说,您可以通过 Assembly.Load< 加载 DLL /a> (或 Assembly.LoadFrom)然后调用 Assembly.GetTypes 然后每种类型调用 Type.GetMethods。当您有 MethodInfo 时,您可以调用 MethodInfo.Invoke 就可以了。

Generally, you use System.Reflection classes to do this task.

Specifically, you'd load the DLL via Assembly.Load (or Assembly.LoadFrom) and then call Assembly.GetTypes and then for each type call Type.GetMethods. When you have a MethodInfo, you can call MethodInfo.Invoke on it.

悲凉≈ 2024-08-26 23:12:54

您需要使用反射

您可以调用 Assembly.LoadFile 加载包含 .Net 程序集的 .DLL,然后对返回的 Assembly 对象调用 GetTypes 方法以查看 DLL 中的类。< br>
找到您感兴趣的类的 Type 对象后,您可以调用其 InvokeMember 方法来调用函数。

请注意,反射可能会非常慢。

You need to use Reflection.

You can call Assembly.LoadFile to load a .DLL containing a .Net assembly, then call the GetTypes method on the returned Assembly object to look at the classes in the DLL.
Once you've found a Type object for the class you're interested in, you can call its InvokeMember method to call a function.

Beware that reflection can be quite slow.

﹂绝世的画 2024-08-26 23:12:54

是的,这是可能的,您只需从加载 dll 开始:

Assembly assembly = Assembly.LoadFrom(assemblyPath);

然后要调用 dll 内的方法,您必须使用 反射

object obj = assembly.CreateInstance(<type.FullName>);

其中 type.FullName 是该程序集中某种类型的 FullName 属性。

获得实例后,您可以像这样调用您的方法:

MethodInfo methodInfo = obj.GetMethod("MyMethod");
methodInfo.Invoke(obj,null);

Yes, this is possible, you just start with loading your dll:

Assembly assembly = Assembly.LoadFrom(assemblyPath);

And then to invoke a method inside your dll you'll have to use reflection.

object obj = assembly.CreateInstance(<type.FullName>);

where type.FullName is the FullName property of some type in that assembly.

Once you got your instance, you can invoke your method like this:

MethodInfo methodInfo = obj.GetMethod("MyMethod");
methodInfo.Invoke(obj,null);
向日葵 2024-08-26 23:12:54

我发现这个在
反射示例

 // get all public static methods of MyClass type
  MethodInfo[] methodInfos = typeof(MyClass).GetMethods(BindingFlags.Public |
                                                  BindingFlags.Static);


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

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

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

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

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

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

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

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

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

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

[C#]
// 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 });

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

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

I found this at
reflection eamples

 // get all public static methods of MyClass type
  MethodInfo[] methodInfos = typeof(MyClass).GetMethods(BindingFlags.Public |
                                                  BindingFlags.Static);


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

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

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

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

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

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

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

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

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

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

[C#]
// 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 });

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

[C#]
// get value of private field: private double _number
double value = (double)calcType.InvokeMember("_number",
BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,
null, calcInstance, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文