C# : Call Class Method Using Name From String


Hello, Today i am going to share a code snippet for how to call a method using string name. Its very simple technique you can easily achieve it using following code Snippet.

Please note hat i have considered the return type of calling function to be string
public string calldynamicMethods(string mehtod, string Parameters)
        {
            try
            {
                Type thisType = this.GetType();
                MethodInfo theMethod = thisType.GetMethod(mehtod);
                object[] a = { (object)Parameters };
                string re = theMethod.Invoke(this, a).ToString();
                return re;
            }
            catch
            {
                return "Internal Server Error";
            }
        }

Thanks and Visit agian.

#Jassie



0 comments:

Post a Comment