反思:如何调用方法与参数 [英] Reflection: How to Invoke Method with parameters

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

问题描述

我试图通过与参数反射调用的方法,我也得到:


  

对象不匹配目标类型


如果我调用一个方法不带参数,它工作正常。基于以下code,如果我调用该方法测试(TestNoParameters),它工作正常。但是,如果我叫测试(运行),我得到一个异常。有什么问题我的code?

我最初的目的是通过例如对象的数组公共无效运行(对象[]选项)但这并没有工作,我想简单的东西如字符串没有成功。

  // Assembly1.dll
命名空间TestAssembly
{
    公共类主
    {
        公共无效运行(字符串参数)
        {
            // 做一点事...
        }
        公共无效TestNoParameters()
        {
            // 做一点事...
        }
    }
}//执行Assembly.exe
公共类TestReflection
{
    公共无效测试(字符串methodName中)
    {
        装配装配= Assembly.LoadFile(... Assembly1.dll);
        类型type = assembly.GetType(TestAssembly.Main);        如果(型!= NULL)
        {
            MethodInfo的MethodInfo的= type.GetMethod(方法名);            如果(MethodInfo的!= NULL)
            {
                对象result = NULL;
                信息参数[] =参数methodInfo.GetParameters();
                对象= ClassInstance的Activator.CreateInstance(类型,NULL);                如果(parameters.Length == 0)
                {
                    //这工作正常
                    结果= methodInfo.Invoke(ClassInstance的,NULL);
                }
                其他
                {
                    [对象] parametersArray =新对象[] {你好};                    //的调用不工作;
                    //它抛出对象不匹配目标类型
                    结果= methodInfo.Invoke(MethodInfo的,parametersArray);
                }
            }
        }
    }
}


解决方案

更改的MethodInfo到ClassInstance的,就像在与空参数数组呼叫。

 结果= methodInfo.Invoke(ClassInstance的,parametersArray);

I am trying to invoke a method via reflection with parameters and I get:

object does not match target type

If I invoke a method without parameters, it works fine. Based on the following code if I call the method Test("TestNoParameters"), it works fine. However if I call Test("Run"), I get an exception. Is something wrong with my code?

My initial purpose was to pass an array of objects e.g. public void Run(object[] options) but this did not work and I tried something simpler e.g. string without success.

// Assembly1.dll
namespace TestAssembly
{
    public class Main
    {
        public void Run(string parameters)
        { 
            // Do something... 
        }
        public void TestNoParameters()
        {
            // Do something... 
        }
    }
}

// Executing Assembly.exe
public class TestReflection
{
    public void Test(string methodName)
    {
        Assembly assembly = Assembly.LoadFile("...Assembly1.dll");
        Type type = assembly.GetType("TestAssembly.Main");

        if (type != null)
        {
            MethodInfo methodInfo = type.GetMethod(methodName);

            if (methodInfo != null)
            {
                object result = null;
                ParameterInfo[] parameters = methodInfo.GetParameters();
                object classInstance = Activator.CreateInstance(type, null);

                if (parameters.Length == 0)
                {
                    // This works fine
                    result = methodInfo.Invoke(classInstance, null);
                }
                else
                {
                    object[] parametersArray = new object[] { "Hello" };

                    // The invoke does NOT work;
                    // it throws "Object does not match target type"             
                    result = methodInfo.Invoke(methodInfo, parametersArray);
                }
            }
        }
    }
}

解决方案

Change "methodInfo" to "classInstance", just like in the call with the null parameter array.

  result = methodInfo.Invoke(classInstance, parametersArray);

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

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