更正参数计数不匹配

本文关键字:不匹配 参数 | 更新日期: 2023-09-27 18:08:40

如何更正此错误我有

用户代码未处理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() });
    }
}

这是整个代码

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

更正参数计数不匹配

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

然后使用InvokeMethod调用MyClass.Method5,它接受两个参数,一个int和一个double。这显然是行不通的,因为myClass.Method5("some string")在语法上是不正确的,而这正是有效发生的。你不能指望字符串是所有MyClass方法的有效参数列表,是吗?

这就是错误的原因,但只有你才能决定如何修复它,因为我们不知道更大的上下文。您必须根据被调用的实际方法提供正确数量的参数。

可能的解决途径:

  • 我想向Method5提供哪些论据
  • 我从哪里买的
  • 如何将它们从它们所在的位置移动到我提供给Invoke的阵列

这应该会让你开始,但没有人能确切地告诉你,因为你只描述了错误,而不是你试图用代码解决的真正问题。

错误不需要任何更正,它是正确的。(

您正试图调用一个方法,该方法使用仅包含一个项的参数数组来获取两个参数。

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

new object[] { 0, 1.5 }

如果您希望InvokeMethod方法与采用不同类型的不同数量参数的方法一起使用,则必须为每个组合创建不同的参数数组。