如何使用Reflection从dll调用方法 [英] How to call a method from a dll using Reflection

查看:92
本文介绍了如何使用Reflection从dll调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包含方法的dll文件,在dll中调用方法的项目文件,异常和我的发现如下,我不明白我缺少什么。请帮帮忙!



dll文件:

dll file containing the method, Project file that calls the method in dll, exception and my findings are below, I do not understand what I'm missing. Please help!

dll file:

public class Class2
    {
        public string TestMethod1()
        {
            return "hello world!";
        }

        public void TestMethod2(int test)
        {
        }
    }



项目在dll中的方法被称为:


Project where Method in dll is called:

try
            {
                Assembly assembly = Assembly.LoadFrom(@"D:\TestDll.dll");                
                foreach (Type ti in assembly.GetTypes().Where(x => x.IsClass))
                {
                    if (ti.Name == "Class2")
                    {
                        ti.GetMethod("TestMethod2");
                        MethodInfo[] method = ti.GetMethods();
                        Type t = assembly.GetType("TestDll.Class2");
                        BindingFlags flags = BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance;
                        object res = t.InvokeMember("TestMethod2", BindingFlags.InvokeMethod, null, t, new object[0]);
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }



异常:


Exception:

MissingMethodException Occurred<br />
Method 'TestDll.Class2.TestMethod2' not found.





我从MethodInfo中得到的结果:



My Findings from MethodInfo:

method[1] = {Void TestMethod2(Int32)};<br />
method[1].Name = TestMethod2





如果我更改最后一个参数,即出现错误后输入参数

例外:



If I change the last parameter i.e. input parameters following error occurs
Exception:

TargetException Occurred<br />
Object does not match target type.

推荐答案

试试这样..





Try like this..


if (ti.Name == "Class2")
           {
               MethodInfo TestMethod1 = ti.GetMethod("TestMethod1");
               MethodInfo TestMethod2 = ti.GetMethod("TestMethod2");
               var instance = Activator.CreateInstance(ti);
               string value = TestMethod1.Invoke(instance, null).ToString();
               TestMethod2.Invoke(instance, new object[] { 1 });

           }


简单:你的方法不是静态的,它们是实例方法:你需要创建一个实例对象并通过它,而不是试图传递类的类型,你需要传递参数:

Simple: your methods are not static, they are instance methods: you need to create an instance of the object and pass that through, rather than trying to pass the type of the class, and you need to pass the argument through:
Assembly assembly = Assembly.LoadFrom(@"D:\TestDll.dll");
foreach (Type ti in assembly.GetTypes().Where(x => x.IsClass))
    {
    if (ti.Name == "Class2")
        {
        Type t = assembly.GetType("TestDll.Class2");
        object obj = t.InvokeMember(null,
                                    BindingFlags.DeclaredOnly |
                                       BindingFlags.Public |
                                       BindingFlags.NonPublic |
                                       BindingFlags.Instance |
                                       BindingFlags.CreateInstance,
                                    null,
                                    null,
                                    null);
        object res = t.InvokeMember("TestMethod2", BindingFlags.InvokeMethod, null, obj, new object[] { (object)13 });
        }
    }


这篇关于如何使用Reflection从dll调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆