locked
Passing parameter(s) to method using Reflection RRS feed

  • Question

  •  

    Hello. I am having trouble finding the solution to my problem. I created instances of two classes using Activator.CreateInstance and Reflection. The first class contains properties that are filled with values from code. The second class saves the class in it by using the "Insert" method. For some reason, I keep getting an error. How would I go about accomplishing this? This is what I have, but it does not seem to work.

     

    Code Snippet

     

    MethodInfo myMethod = t2.GetMethod("Insert"); //Gets the insert method in the 2nd class

     

    myMethod.Invoke(Class2Object,Class1Object);

     

    It gives me the error: "Cannot convert object to object[]." Is there a way around this?

     

    Wednesday, July 11, 2007 2:52 PM

Answers

  • You pass Parameters via an Object[] { }

    I would take a guess and determine that

    Code Snippet

    myMethod.Invoke(t2, new Object[] { Class2Object, Class1Object } );

     

    Is what you are looking for. This assumes that the method takes 2 parameters and that class2object and class1object are and that t2 is the instance to the class that you wish to execute the method for.

     

    Hope this helps.

     

    Wednesday, July 11, 2007 2:55 PM

All replies

  • You pass Parameters via an Object[] { }

    I would take a guess and determine that

    Code Snippet

    myMethod.Invoke(t2, new Object[] { Class2Object, Class1Object } );

     

    Is what you are looking for. This assumes that the method takes 2 parameters and that class2object and class1object are and that t2 is the instance to the class that you wish to execute the method for.

     

    Hope this helps.

     

    Wednesday, July 11, 2007 2:55 PM
  • Thank you, that solved my problem. I appreciate the help.
    Wednesday, July 11, 2007 3:07 PM