1

Let's say I have a page Test.aspx along with test.aspx.vb.

Test.aspx.vb contains a class name "TestClass". In that class I have method1(), method2() and method3()

I need to be able to call one of those methods, but I can't hard code it, the method to be executed comes from a string.

I can't do

Select Case StringContainingTheNameOfTheDesiredMethod
    Case "Method1" 
        Method1()
    Case "Method2"
       Method2()
end case

.

That I could find how to do with reflection (I followed that example). My problem is that those methods might need to interact with test.aspx, but when I use .invoke it seems to create a new thread or context and any reference to test.aspx becomes null (setting label1.text = "something" will generate a null reference, but a direct call of method1 (without invoke) will update label1.text just fine.

Is there any solution ? Can anyone give me some tips?

4
  • What exactly can't you hard code? The method string, or the method call itself? Commented Oct 5, 2011 at 22:15
  • The only solution I can find is to call a method that returns an object and use that object as a data source to interact with test.aspx, but since all the methods are already in test.aspx.vb, it would be simplier if I could simply interact with test.aspx within those methods. Commented Oct 5, 2011 at 22:16
  • I need to find a way to avoid that select case (or if statement) to call ? method. I have a string containing the name of the method that needs to be called, In a perfect world I would simply do something like excecuteMethod(StringWithMethodName) Commented Oct 5, 2011 at 22:18
  • The reflection method is the way to do it. You need to elaborate on what losing reference to test.aspx means. Commented Oct 5, 2011 at 22:38

2 Answers 2

1

You need to pass an instance of Test page to Invoke method (so you invoke it on the object). Sorry for C# code ;-)

MethodInfo method = typeof(TestPage).GetMethod(StringContainingTheNameOfTheDesiredMethod);
method.Invoke(this, null);
Sign up to request clarification or add additional context in comments.

2 Comments

Well, it helped me find the answer, but its not the exact answer ... so I upvoted your comment and answer.
You might want to update your question with the final solution to help others.
1
    Dim xAssembly As Assembly = Assembly.GetExecutingAssembly()

    Dim xClass As Object = xAssembly.CreateInstance("Paradox.Intranet2.ManageUsers", False, BindingFlags.ExactBinding, Nothing, New Object() {}, Nothing, Nothing)
    Dim xMethod As MethodInfo = xAssembly.GetType("Paradox.Intranet2.ManageUsers").GetMethod("TestCallFromString")

    Dim ret As Object = xMethod.Invoke(Me, New Object() {})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.