如何纠正参数计数不匹配

tru*_*rue 10 c# reflection

我怎样才能纠正我遇到的这个错误

用户代码未处理TargetParameterCountException.参数计数不匹配.

这是我正在发生的代码

public static void InvokeMethod(string className, string methodName, string fileName)
{
    var t = Type.GetType(className);
    using (StreamReader f = new StreamReader("params.txt"))
    {
        t.GetMethod(methodName).Invoke(t.GetConstructor(Type.EmptyTypes).Invoke(new object[] { }), new object[] { f.ReadLine() });
    }
}
Run Code Online (Sandbox Code Playgroud)

这是整个代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.IO;

class MyClass
{
    private int i;
    public double d;
    private string s;
    public bool b;
    public MyClass()
    {
        i = 1;
        d = 0.1;
        s = "1";
        b = true;
    }
    public void Method0()
    {
        Console.WriteLine("Method with no arguments, no return value.");
    }
    private int Method1(int arg0)
    {
        Console.WriteLine("The method returns int, int gets.");
        return arg0;
    }
    private double Method2(int arg0, double arg1)
    {
        Console.WriteLine("Method returns a double, taking int and double.");
        return arg1 * arg0;
    }
    public bool Method3(string arg0)
    {
        Console.WriteLine("Method returns a bool, accepts string");
        return arg0.Length>10;
    }
    public bool Method3(string arg0,string arg1)
    {
        Console.WriteLine("The method takes two arguments string.");
        return arg0 == arg1;
    }
    public static char Method4(string arg0)
    {
        Console.WriteLine("Method returns a char, accepts string. .");
        Console.WriteLine(arg0);
        return arg0[1];
    }
    public void Method5(int arg0, double arg1)
    {
        Console.WriteLine("arg1 = {0} arg2 = {1}.",arg0,arg1);
    }
}

class MyTestClass
{
    public static string[] GetMethodsWithStrParams(string className)
    {
        var t = Type.GetType(className);
        List<string> res = new List<string>();
        foreach (var method in t.GetMethods())
        {
            foreach (var param in method.GetParameters())
            {
                if (param.ParameterType == typeof(string))
                {
                    res.Add(method.Name);
                    break;
                }
            }
        }
        return res.ToArray();
    }
    public static void InvokeMethod(string className, string methodName, string fileName)
    {
        var t = Type.GetType(className);
        using (StreamReader f = new StreamReader("params.txt"))
        {
            t.GetMethod(methodName).Invoke(t.GetConstructor(Type.EmptyTypes).Invoke(new object[] { }),
                                           new object[] { f.ReadLine() });
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        string name = "MyClass";

        foreach (var x in MyTestClass.GetMethodsWithStrParams(name))
        {
            Console.WriteLine(x);
        }

        MyTestClass.InvokeMethod("MyClass", "Method5", "params.txt");

        Console.ReadKey(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

Zde*_*vic 11

您的InvokeMethod实现总是t.GetMethod(methodName).Invoke使用两个参数调用,第一个是调用该方法的目标实例,第二个是方法参数数组,它只包含一个字符串(f.ReadLine()).

然后你InvokeMethod用来调用MyClass.Method5哪个接受两个参数,一个是int和一个double.这显然不起作用,因为myClass.Method5("some string")语法不正确,这就是有效发生的事情.您不能指望字符串是所有MyClass方法的有效参数列表,可以吗?

这是错误的原因,但只有你可以决定如何解决它,因为我们不知道更大的上下文.您必须提供正确数量的参数,具体取决于所调用的实际方法.

可能的解决方案:

  • 我想为Method5提供什么参数?
  • 我从哪里得到它们?
  • 如何将它们从任何地方移动到我给出的数组 Invoke

这应该可以帮助您入门,但没有人可以准确地告诉您,因为您只描述了错误,而不是您尝试使用代码解决的真正问题.


Guf*_*ffa 5

该错误不需要任何修正,这是正确的.;)

您正在尝试调用一个方法,该方法接受两个参数,其中参数数组仅包含一个项目.

适用于该特定方法的参数数组例如是:

new object[] { 0, 1.5 }
Run Code Online (Sandbox Code Playgroud)

如果希望InvokeMethod方法使用具有不同类型的不同参数的方法,则必须为每个组合创建不同的参数数组.