简体   繁体   English

C#反射调用-无法将类型为“ XXX”的对象转换为类型为“ System.Object []”的对象

[英]C# Reflection Invoke - Object of Type 'XXX' Cannot Be Converted to type 'System.Object[]'

I have created an instance called input that is of type: 我创建了一个名为input的实例,其类型为:

public class TestInput
{
    public int TesTInt { get; set; }
}

I use this in this function: 我在此功能中使用此功能:

public static class TestClass
{
    public static string TestFunction()
    {
        var testInput = new TestInput();
        string res = ServicesManager.Execute<string>((object) testInput);

        return res;
    }
}

The Execute function is here: Execute功能在这里:

public static OUT Execute<OUT>(object input) 
            where OUT : class
{
       var method = //getting method by reflection
       object[] arr = new object[] { input };
       return method.Invoke(null, arr) as OUT; //Error is triggered here
}

The method that I invoke is this one: 我调用的方法是这样的:

public static string TestFunctionProxy(object[] input)
{
       var serviceInput = input[0] as TestInput;
       //rest of code
}

I received the error in the title. 我收到标题错误。 (XXX - "TestInput" type) (XXX-“ TestInput”类型)

What's happening and what is causing this error? 发生什么情况以及导致此错误的原因是什么?

Note: method is static so no instance is required for the first parameter. 注意: method是静态的,因此第一个参数不需要实例。 Please correct me if I'm wrong. 如果我错了,请纠正我。

Any help is appreciated. 任何帮助表示赞赏。

EDIT: Updated the question with some more code for a complete example. 编辑:使用更多代码更新了问题,以获取完整的示例。

You are passing the wrong arguments to the method. 您将错误的参数传递给该方法。 It wants an object[] and you are giving a simpe object. 它需要一个object [],而您要提供一个simpe对象。 This is how to fix it: 这是解决方法:

object[] arr = new object[] { new object[] { input } };

The 'outer' object[] is the parameter for Invoke, the 'inner' array is the parameter for your method. “外部”对象[]是Invoke的参数,“内部”数组是您的方法的参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 异常“类型&#39;System.Object []&#39;的对象不能转换为类型” - Exception “Object of type 'System.Object[]' cannot be converted to type” 无法将类型为“ System.Reflection.MdFieldInfo”的对象转换为类型为“ MyEnum”的对象 - Object of type 'System.Reflection.MdFieldInfo' cannot be converted to type 'MyEnum' C# 未定义或导入预定义类型“System.Object” - C# Predefined type 'System.Object' is not defined or imported Nhibernate抛出&#39;System.Linq.EnumerableQuery`1 [Entity]&#39;类型的对象无法转换为类型&#39;System.Linq.IQueryable`1 [System.Object []]&#39; - Nhibernate throws Object of type 'System.Linq.EnumerableQuery`1[Entity]' cannot be converted to type 'System.Linq.IQueryable`1[System.Object[]]' C#反射调用-返回通用对象{类型}-需要类型 - C# Reflection Invoke - Returns Generic Object { Type } - Need Type 无法转换System.Object []错误 - System.Object[] error cannot be converted 无法将“System.Object[]”类型的对象转换为我的类 C# - Unable to cast object of type 'System.Object[]' to type my class C# 无法将类型为“ System.Reflection.ParameterInfo”的对象转换为类型为“ System.Int32”的对象 - Object of type 'System.Reflection.ParameterInfo' cannot be converted to type 'System.Int32' C#System.Object [,] - C# System.Object[,] 无法将“ System.Object []”转换为类型“ System.String []” - Cannot convert 'System.Object[]' to the type 'System.String[]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM