Click here to Skip to main content
15,886,802 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
is it possible to call a function at runtime with the string variable??

I created a form in which one textBox and a Button. In my code i Created a function name is CallMe(). I want when the textBox text is "CallMe()" and then the button clicked, that function automatically Call...

Thanks in Advance.....
Posted
Updated 14-Jan-13 3:27am
v2
Comments
Sandeep Mewara 14-Jan-13 9:28am    
Not clear. Please re-phrase/elaborate.
David_Wimbley 14-Jan-13 9:28am    
Your question is not very clear, what platform is this? Asp.net? Winforms? WPF?

It should be a matter of attaching a click event to the button, whether its Winforms/WPF/Asp.net but it would be nice to know for sure what platform.
JayantaChatterjee 14-Jan-13 9:34am    
the platform is winforms..
JayantaChatterjee 14-Jan-13 9:38am    
I mean, if at runtime i type "CallMe()" in textBox and then Clicked the button, the function automatically which name match that variable value.....
Please Help me..............

I ran this and it will call the ShowMessage method on click from the button1 event

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        public static void ShowMessage()
        {
            MessageBox.Show("Called Me Dynamically!");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            object obj2 = typeof(Form1).GetMethod("ShowMessage").Invoke(null, null);
        }
    }
}
 
Share this answer
 
Comments
JayantaChatterjee 14-Jan-13 11:52am    
thank a lotttttttttttttttttt Sir....

is it possible to call nonstatic methods ??
David_Wimbley 14-Jan-13 11:56am    
Your welcome, happy to help.

It is possible to call nonstatic methods, i just did the easiest/less confusing way of going about doing dynamic invoking of methods
JayantaChatterjee 14-Jan-13 12:06pm    
nice to meet Sir... :-)

can you tell me how to do this(call nonstatic methods)???
I think you mean that if the user types the name of a function in the textbox and presses a button, that the text he typed should be treated as code and executed, yes?

If so, then it is possible, but I would very strongly recommend against it. What if what they type is
File.Delete(@"%USERPROFILE%\*.*");
Would it be a good idea to execute that? And there are much, much worse things they could do...

Please, think again about what you want to do - there are some programs you shouldn't let your mates get anywhere near...
 
Share this answer
 
Comments
JayantaChatterjee 14-Jan-13 9:43am    
I appreciate your suggestion...
but i want to call function which i defined in my code...
This is how to dynamically call a method at runtime.

C#
Type type = Type.GetType(type);
object obj = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod("YourMethodNameGoesHere");
method.Invoke(obj, null);


I just hammered that out real quick, not sure if it works but should point you in right direction

To call with parameters

C#
object[] paramsArray = new object[] { "Im A String Param" };
methodInfo.Invoke(method, paramsArray);
 
Share this answer
 
v3
Comments
JayantaChatterjee 14-Jan-13 9:53am    
I'm getting error ->

Type type=Type.GetType(type);//The best overloaded method match for 'System.Type.GetType(string)' has some invalid arguments

can u tell me which directive I use for MethodInfo???
David_Wimbley 14-Jan-13 10:00am    
Unless you post your code i have no idea what your type is, its Namespace.Classname.
JayantaChatterjee 14-Jan-13 10:03am    
My Code is ->

private void okBtn_Click(object sender, EventArgs e)
{
Type type = Type.GetType(type);
object obj = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod(textBox1.Text);
method.Invoke(obj, null);
}
private void CallMe()
{
MessageBox.Show("In Call Me..");
}
David_Wimbley 14-Jan-13 10:03am    
Can you post your namespace/classname?
JayantaChatterjee 14-Jan-13 10:06am    
using System;
My namespaces -:
using System.Windows.Forms;
using System.Reflection;

Error is -: Type type=Type.GetType(type);//The best overloaded method match for 'System.Type.GetType(string)' has some invalid arguments
Hello
i Got Your Query You Want Whent You enter Text In textbox means Function Name And Click Button it will Invoke Your Function.


So'

For this You Can Click button curser transfer your btn Event
then you check What you type in your textbox and invoke your function like:-
CallMe()

and
perform your function.
but also know your function call back your button click event .
 
Share this answer
 
Comments
JayantaChatterjee 22-Jan-13 7:24am    
I want its totaly depend on users input(Dynamically) invoked function...
rizwan muhammed khan gouri 22-Jan-13 7:44am    
Use principle of thread

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900