我试图通过反射参数调用一个方法,我得到:

对象与目标类型不匹配

如果我调用一个没有参数的方法,它可以正常工作。根据下面的代码,如果我调用方法Test(“TestNoParameters”),它可以正常工作。然而,如果我调用测试(“运行”),我得到一个异常。我的代码有问题吗?

我最初的目的是传递一个对象数组,例如公共void Run(object[] options),但这并不奏效,我尝试了一些更简单的方法,例如字符串没有成功。

// 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);
                }
            }
        }
    }
}

当前回答

将“methodInfo”更改为“classInstance”,就像对空参数数组的调用一样。

  result = methodInfo.Invoke(classInstance, parametersArray);

其他回答

我会像这样使用它,它更短,不会出任何问题

        dynamic result = null;
        if (methodInfo != null)
        {
            ParameterInfo[] parameters = methodInfo.GetParameters();
            object classInstance = Activator.CreateInstance(type, null);
            result = methodInfo.Invoke(classInstance, parameters.Length == 0 ? null : parametersArray);
        }

将“methodInfo”更改为“classInstance”,就像对空参数数组的调用一样。

  result = methodInfo.Invoke(classInstance, parametersArray);

这是一个根本性的错误:

result = methodInfo.Invoke(methodInfo, parametersArray); 

您正在MethodInfo的实例上调用该方法。您需要传入您想要调用的对象类型的实例。

result = methodInfo.Invoke(classInstance, parametersArray);

这里有个虫子

result = methodInfo.Invoke(methodInfo, parametersArray);

应该是这样

result = methodInfo.Invoke(classInstance, parametersArray);

所提供的解决方案不适用于从远程程序集加载的类型的实例。为此,这里有一个适用于所有情况的解决方案,它涉及到通过CreateInstance调用返回的类型的显式类型重映射。

这就是我需要如何创建我的classInstance,因为它位于远程程序集中。

// sample of my CreateInstance call with an explicit assembly reference
object classInstance = Activator.CreateInstance(assemblyName, type.FullName); 

但是,即使使用上面提供的答案,仍然会得到相同的错误。以下是如何着手的:

// first, create a handle instead of the actual object
ObjectHandle classInstanceHandle = Activator.CreateInstance(assemblyName, type.FullName);
// unwrap the real slim-shady
object classInstance = classInstanceHandle.Unwrap(); 
// re-map the type to that of the object we retrieved
type = classInstace.GetType(); 

然后像这里提到的其他用户那样做。