Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 697
  • Last Modified:

Passing jagged Array parameters to InvokeMember

Hi there,
I have a few methods which I need to invoke dynamically - some of the parameters to the method are arrays and my attempts at passing them in the Object [] just fail - can anybody let me know how to do this please?
Attached is a totally stripped down class, just to give an idea of what I'm trying to do.

Thanks,
Leo
using System;
using System.Reflection;
using System.IO;



namespace CorrFrameworkRunner
{
    class FrameworkRunner
    {

        static void Main(string[] args)
        {
            try
            {
            

                Type test = typeof (TestParams);
                ParameterInfo [] p = test.GetMethod("ArrayTest").GetParameters();
                
                test.InvokeMember("ArrayTest", BindingFlags.InvokeMethod, null, test, new object[]
                                                                                          {
                                                                                              "firstval", "secondval",
                                                                                              new string[2]
                                                                                                  {"arr1", "arr2"}
                                                                                          });

            }
            catch (Exception e)
            {
                Console.WriteLine("Improper exit - Exception returned:" + e.Message);                
                return;
            }
        }
    }
    
    class TestParams
    {
        public TestParams() {}
        public void ArrayTest(string a1,string a2,string [] a3)
        {
            Console.WriteLine("Parameters {0},{1},{2}",a1,a2,a3[0],a3[1]);
            
        }
    }
}

Open in new window

0
lm78
Asked:
lm78
1 Solution
 
saraganiCommented:
           TestParams testParams = new TestParams();            test.InvokeMember("ArrayTest", BindingFlags.InvokeMethod, null, testParams, new object[]
                                                                                          {
                                                                                              "firstval", "secondval",
                                                                                              new string[2]
                                                                                                  {"arr1", "arr2"}
                                                                                          });


You were sending the Type instead of an Instance as the Target Object
0
 
lm78Author Commented:
how stupid, i actually knew that! was so sure i was messing up the arguements that i never saw this!
thanks, that worked!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now