Link to home
Start Free TrialLog in
Avatar of NBAIS
NBAIS

asked on

Calling a Method by it's string name and pass variables

How do I use the following system.reflection code to call a method by it's string name and pass that method a variable?

Imports System.Reflection

Public Class Reflect

    Public Shared Function ExecuteMethod(ByVal name As String, ByVal parameters() As Object) As Object
        Dim assy As Assembly = [Assembly].GetExecutingAssembly()
        Dim ns As String = assy.GetType().Namespace

        Dim method As MethodInfo = assy.GetType().GetMethod(ns & "." & name)
        Return method.Invoke(Nothing, parameters)
    End Function

End Class
Avatar of brandonvmoore
brandonvmoore

This isn't the answer that you want to hear I'm sure, but it's likely the best answer for you nonetheless:

If you working on a project that had some special case where you actually needed to do this, then you would also have enough experience to read the documentation and learn how to do it for yourself.  The very fact that you are asking this question tells me that whatever you are trying to do could be implemented in a better way.

Someone else will probably answer your question more directly, but that doesn't mean that they've given you the right answer for what you need to do.

Good luck.
ASKER CERTIFIED SOLUTION
Avatar of CuteBug
CuteBug
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NBAIS

ASKER

Thank you!