2013-04-10 85 views
9

我怎样才能纠正这个错误我有如何更正参数数量不匹配

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

疯狂的猜测,但这是当它运行到一个方法来调用多个参数? – 2013-04-10 12:55:55

+0

看到这个问题http://stackoverflow.com/questions/3721782/parameter-count-mismatch-with-invoke – 2013-04-10 12:56:29

+0

几个问题:你为'InvokeMethod'指定一个文件名参数,但忽略它,并且总是使用“params”。文本”; 'new object [] {f.ReadLine()}'将总是产生一个包含一个元素(一个字符串)的对象数组;并且您将传递固定数量的参数给任何您调用的方法,即使您的类中的每个方法都使用不同的参数。 – yoozer8 2013-04-10 12:58:41

回答

10

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

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

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

可能的路径解决方案:

  • 什么是我希望提供给Method5的论点?
  • 我从哪里得到它们?
  • 我应该如何将它们从任何地方移动到我给予Invoke的阵列?

这应该让你开始,但没有人能完全告诉你,因为你只描述错误,但不是你想要用你的代码解决的真正问题。

+0

我该如何解决它,你是对的,但我不知道如何修复它 – trupatrue 2013-04-10 13:01:50

+0

我不能告诉你,因为我不知道你到底想要达到什么目的。我不知道什么样的论点适合你的方法,你从哪里拿走它们。 – 2013-04-10 13:04:54

+0

让我重新发布我解释的问题 – trupatrue 2013-04-10 13:17:12

4

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

您正尝试调用一个方法,该方法只接受一个包含一个项目的参数数组的两个参数。

参数数组会针对特定方法的工作将是例如:

new object[] { 0, 1.5 } 

如果你希望你的InvokeMethod方法与采取不同数量的不同类型参数的方法来工作,你必须创建每个组合的不同参数数组。

+0

,因为它是'Method5'特有的示例,不会因某些参数类型错误而失败,因为第二个参数是double? – 2013-04-10 12:59:23

+0

如果我把method4它可以从文本中读取,但不能与method5,我试着用你的例子,但我仍然得到系统错误 – trupatrue 2013-04-10 12:59:56

+0

@ZdeslavVojkovic:是的,你是对的,我纠正了这个例子。 – Guffa 2013-04-10 13:00:50