none
为什么我在反射中运用变长参数出现如下错误 RRS feed

  • Question

  • 操作的方法如下:

     

      public void Add( params int [] a  )

            {

     

                int sum = 0;

                foreach (int a1 in a)

                {

                    sum += a1;

                    Console.Write("{0}+", a1);

                }

                Console.WriteLine("=" + sum);

            }

     

     

    主函数的代码如下:

     

     object obj = Activator.CreateInstance(type);

                BindingFlags flags = (BindingFlags.NonPublic |

                    BindingFlags.Public | BindingFlags.Static |

                    BindingFlags.Instance | BindingFlags.DeclaredOnly);

                //(3)通过方法名和范围查找方法,用say保存方法的信息

                MethodInfo say = type.GetMethod(Console.ReadLine(), flags);

     

                //(4)用say调用方法,调用时要指定调用哪一个实例的方法

                Console.WriteLine("加数个数");

                int p = Convert.ToInt32(Console.ReadLine());

                object[] x = new Object[] { 1,2 };//{ Console.ReadLine()};

     say.Invoke(obj, x);

    为什么会出现参数计数不匹配!

    Wednesday, November 17, 2010 8:08 AM

Answers

  • OK, although I don't speak chinese, an online translator was able to tell me that you are getting the error "parameter count does not match". This would happen in the "say.Invoke" method.

    "say" refers to the Console.Readline method. And "x" should contain the parameters being passed to the method. Since x is an Object array with two values, you are trying to pass two parameters to Console.Readline. But there is no overload of ReadLine taking two parameters, in fact it can only take zero parameters. Hence, the "parameter count does not match".

    If you need to discuss Reflection further, then, as Konrad suggested, it would be best to do that in a .Net development forum rather than the Training and Certification forum.

    Thursday, November 18, 2010 8:26 PM

All replies

  • Hi,

    you seem to have some question regarding reflection, but I am unable to understand you at all. Is it possible to write us in english, what you try?

    And maybe you want to switch to a C# forum because your question seems to be c# related and not related to an certification or training.

    With kind regards,

    Konrad

    Wednesday, November 17, 2010 8:46 AM
    Answerer
  • OK, although I don't speak chinese, an online translator was able to tell me that you are getting the error "parameter count does not match". This would happen in the "say.Invoke" method.

    "say" refers to the Console.Readline method. And "x" should contain the parameters being passed to the method. Since x is an Object array with two values, you are trying to pass two parameters to Console.Readline. But there is no overload of ReadLine taking two parameters, in fact it can only take zero parameters. Hence, the "parameter count does not match".

    If you need to discuss Reflection further, then, as Konrad suggested, it would be best to do that in a .Net development forum rather than the Training and Certification forum.

    Thursday, November 18, 2010 8:26 PM