invoke a method by reflection£¬the method's parameters can not be ArrayList?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jerry051

    invoke a method by reflection£¬the method's parameters can not be ArrayList?

    I invoke a method by reflection, when this method's parameters is simple
    type like int or string,the invoking is correct and secceed. But when the
    parameters is ArrayList type, debuger tips me that meet
    System.Reflecti on.TargetParame terCountExcepti on error, and parameters count
    is not matching. please help me why happen this error,thanks a lot!


  • Oliver Sturm

    #2
    Re: invoke a method by reflectionï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½

    jerry051 wrote:
    [color=blue]
    > I invoke a method by reflection, when this method's parameters is simple
    > type like int or string,the invoking is correct and secceed. But when the
    > parameters is ArrayList type, debuger tips me that meet
    > System.Reflecti on.TargetParame terCountExcepti on error, and parameters count
    > is not matching. please help me why happen this error,thanks a lot![/color]

    Please show us the code you are using when you invoke the method.


    Oliver Sturm
    --
    omnibus ex nihilo ducendis sufficit unum
    Spaces inserted to prevent google email destruction:
    MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
    ICQ 27142619 http://www.sturmnet.org/blog

    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: invoke a method by reflection£¬the method's parameters can not be ArrayList?

      Hi,

      Show the code, you should use the very same approach , create an object[]
      and construct it with your parameters. it should work fine


      cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation



      "jerry051" <bestsoft@sogou .com> wrote in message
      news:%23XryqBol FHA.2444@tk2msf tngp13.phx.gbl. ..[color=blue]
      >I invoke a method by reflection, when this method's parameters is simple
      > type like int or string,the invoking is correct and secceed. But when the
      > parameters is ArrayList type, debuger tips me that meet
      > System.Reflecti on.TargetParame terCountExcepti on error, and parameters
      > count
      > is not matching. please help me why happen this error,thanks a lot!
      >
      >[/color]


      Comment

      • Oliver Sturm

        #4
        Re: invoke a method by reflectionï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½

        jerry051 wrote:

        Okay. The reason for your problem lies in these two lines:
        [color=blue]
        > public object Exec(ArrayList parameters)[/color]

        The Exec method is declared to receive one parameter: An ArrayList.
        [color=blue]
        > object temp = myType.GetMetho d("Exec").Invok e(myInstance, new
        > object[]{"a","b","c","d ","e"}); //this will cause exception that i
        > describing ahead![/color]

        But your call passes five parameters, not one. This doesn't fit,
        obviously. Maybe you were expecting some automagical conversion to take
        place, well, bad luck in that case :-)

        You'll have to do one of two things. You can (a) create an arraylist to
        pass to your method:

        ArrayList list = new ArrayList(new string[] {
        "a","b","c","d" ,"e" });
        object temp = myType.GetMetho d("Exec").Invok e(myInstance,
        new object[]{ list });

        Or your can change your Exec method to take a list of parameters instead
        of the arraylist:

        public object Exec(params object[] parameters) {
        ...


        Oliver Sturm
        --
        omnibus ex nihilo ducendis sufficit unum
        Spaces inserted to prevent google email destruction:
        MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
        ICQ 27142619 http://www.sturmnet.org/blog

        Comment

        • jerry051

          #5
          Re: invoke a method by reflectionï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿

          Oliver Sturm: hi,thank u very much ,i have dealed this proplem :)
          you are right, it's my inattention with parameters pass.


          "jerry051" <bestsoft@sogou .com> дÈëÓʼþ
          news:uV0xhVwlFH A.2920@TK2MSFTN GP14.phx.gbl...[color=blue]
          > I accept your advise, changed my method parameters ,but it still report
          > same error! could you help me to read source code (accessory ) look for
          > where is incorrect?
          >
          > "Oliver Sturm" <oliver@sturmne t.org> дÈëÓʼþ
          > news:%239eDq1pl FHA.3144@TK2MSF TNGP12.phx.gbl. ..[color=green]
          > > jerry051 wrote:
          > >
          > > Okay. The reason for your problem lies in these two lines:
          > >[color=darkred]
          > > > public object Exec(ArrayList parameters)[/color]
          > >
          > > The Exec method is declared to receive one parameter: An ArrayList.
          > >[color=darkred]
          > > > object temp = myType.GetMetho d("Exec").Invok e(myInstance, new
          > > > object[]{"a","b","c","d ","e"}); //this will cause exception that i
          > > > describing ahead![/color]
          > >
          > > But your call passes five parameters, not one. This doesn't fit,
          > > obviously. Maybe you were expecting some automagical conversion to take
          > > place, well, bad luck in that case :-)
          > >
          > > You'll have to do one of two things. You can (a) create an arraylist to
          > > pass to your method:
          > >
          > > ArrayList list = new ArrayList(new string[] {
          > > "a","b","c","d" ,"e" });
          > > object temp = myType.GetMetho d("Exec").Invok e(myInstance,
          > > new object[]{ list });
          > >
          > > Or your can change your Exec method to take a list of parameters instead
          > > of the arraylist:
          > >
          > > public object Exec(params object[] parameters) {
          > > ...
          > >
          > >
          > > Oliver Sturm
          > > --
          > > omnibus ex nihilo ducendis sufficit unum
          > > Spaces inserted to prevent google email destruction:
          > > MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
          > > ICQ 27142619 http://www.sturmnet.org/blog[/color]
          >
          >
          >[/color]


          Comment

          • Oliver Sturm

            #6
            Re: invoke a method by reflectionï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½ï¿ ½ï¿½ï¿½ï¿½ï¿½

            Good to hear you figured it out.



            Oliver Sturm
            --
            omnibus ex nihilo ducendis sufficit unum
            Spaces inserted to prevent google email destruction:
            MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
            ICQ 27142619 http://www.sturmnet.org/blog

            Comment

            Working...