invoke methods with params

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • codefragment@googlemail.com

    invoke methods with params

    Hi
    I want to use reflection to call a method which accepts "params
    string[]" as an argument

    I've searched this group and found a better idea than invoke, just
    cast to an interface and then call the method
    directly, but I would still like to know if this is possible, e.g.
    something like the below

    ta
    C




    public class TestA
    {
    public void Meth(params string[] args)
    {
    }
    }

    public partial class _Default : System.Web.UI.P age
    {
    protected void Page_Load(objec t sender, EventArgs e)
    {
    Type handlerInvoke = Type.GetType("R eflectionProjec t.TestA");
    object targetToInvoke = Activator.Creat eInstance(handl erInvoke);

    MethodInfo methodToInvoke = handlerInvoke.G etMethod("Meth" ,
    BindingFlags.In stance | BindingFlags.Pu blic);

    string[] myArgs = {"a", "b", "c"};

    if (methodToInvoke != null)
    {
    try
    {

    object[] args = new object[myArgs.Length];
    for (int i = 0; i < myArgs.Length; i++)
    {
    args[i] = myArgs[i];
    }
    //object[] args = myArgs;

    bool invokeResult = (bool)methodToI nvoke.Invoke(ta rgetToInvoke,
    args);
    }
    catch (Exception la)
    {
    int a = 0; a++;
    }
    }
    }
  • Marc Gravell

    #2
    Re: invoke methods with params

    Yes it is possible, but you need to double-wrap the args - i.e. you
    need:

    string[] paramsArgs = {...blah...}

    object[] methodArgs = {paramsArgs};

    That way, it (correctly) treats it as a single argument that is an
    array, rather than "n" arguments.

    Marc

    Comment

    • Marc Gravell

      #3
      Re: invoke methods with params

      Like so:

      using System;
      using System.Reflecti on;

      static class Program
      {
      static void Main()
      {
      MethodInfo method = typeof(Program) .GetMethod("Tes t");
      string[] values = { "foo", "bar" };
      object[] args = { values };
      method.Invoke(n ull, args);
      }
      public static void Test(params string[] values)
      {
      foreach (string val in values)
      {
      Console.WriteLi ne(val);
      }
      }
      }

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: invoke methods with params

        <codefragment@g ooglemail.comwr ote:
        I want to use reflection to call a method which accepts "params
        string[]" as an argument
        >
        I've searched this group and found a better idea than invoke, just
        cast to an interface and then call the method
        directly, but I would still like to know if this is possible, e.g.
        something like the below
        Sort of - but you've got it slightly wrong. You're trying to invoke a
        method with several string parameters, instead of with a single string
        array parameter.

        Try:

        object[] args = new object[] { myArgs };


        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        World class .NET training in the UK: http://iterativetraining.co.uk

        Comment

        • codefragment@googlemail.com

          #5
          Re: invoke methods with params

          I did try this, I get an "object reference not set" exception, i.e.

          namespace ReflectionProje ct
          {

          public class TestA
          {
          public void Meth(params string[] args)
          {
          }
          }

          public partial class _Default : System.Web.UI.P age
          {
          protected void Page_Load(objec t sender, EventArgs e)
          {
          Type handlerInvoke = Type.GetType("R eflectionProjec t.TestA");
          object targetToInvoke = Activator.Creat eInstance(handl erInvoke);

          MethodInfo methodToInvoke = handlerInvoke.G etMethod("Meth" ,
          BindingFlags.In stance | BindingFlags.Pu blic);

          string[] myArgs = {"a", "b", "c"};

          if (methodToInvoke != null)
          {
          try
          {

          object[] args = { myArgs };


          bool invokeResult = (bool)methodToI nvoke.Invoke(ta rgetToInvoke,
          args);
          }
          catch (Exception la)
          {
          int a = 0; a++;
          }
          }
          }
          }
          }

          Comment

          • Marc Gravell

            #6
            Re: invoke methods with params

            The error is because you are trying to cast the result of a "void"
            method to bool. Either ignore the result, or make the method return a
            bool.

            Marc

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: invoke methods with params

              <codefragment@g ooglemail.comwr ote:
              I did try this, I get an "object reference not set" exception, i.e.
              Well, it would really help if you'd give a short but complete example
              which *doesn't* need to be run in ASP.NET...

              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              World class .NET training in the UK: http://iterativetraining.co.uk

              Comment

              • codefragment@googlemail.com

                #8
                Re: invoke methods with params

                On 23 Apr, 21:48, Marc Gravell <marc.grav...@g mail.comwrote:
                The error is because you are trying to cast the result of a "void"
                method to bool. Either ignore the result, or make the method return a
                bool.
                >
                Marc
                Doh! Your quite right, thats a result of trying many things over time
                and then dozily not noticing. Works now, thanks.

                Comment

                Working...