Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 14-Jan-13 4:25am
Edited 14-Jan-13 4:27am
v2
Comments
Sandeep Mewara at 14-Jan-13 9:28am
   
Not clear. Please re-phrase/elaborate.
David_Wimbley at 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.
Jayanta Chatterjee at 14-Jan-13 9:34am
   
the platform is winforms..
Jayanta Chatterjee at 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..............
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I ran this and it will call the ShowMessage method on click from the button1 event
 
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);
        }
    }
}
  Permalink  
Comments
Jayanta Chatterjee at 14-Jan-13 11:52am
   
thank a lotttttttttttttttttt Sir....
 
is it possible to call nonstatic methods ??
David_Wimbley at 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
Jayanta Chatterjee at 14-Jan-13 12:06pm
   
nice to meet Sir... :-)
 
can you tell me how to do this(call nonstatic methods)???
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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...
  Permalink  
Comments
Jayanta Chatterjee at 14-Jan-13 9:43am
   
I appreciate your suggestion...
but i want to call function which i defined in my code...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This is how to dynamically call a method at runtime.
 
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
 
object[] paramsArray = new object[] { "Im A String Param" };
methodInfo.Invoke(method, paramsArray);
  Permalink  
v3
Comments
Jayanta Chatterjee at 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 at 14-Jan-13 10:00am
   
Unless you post your code i have no idea what your type is, its Namespace.Classname.
Jayanta Chatterjee at 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 at 14-Jan-13 10:03am
   
Can you post your namespace/classname?
Jayanta Chatterjee at 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
David_Wimbley at 14-Jan-13 10:08am
   
No i mean the namespace of your App.
 
Example
 
namespace DavidWimbley.Application.App
{
public class DavidsClass
{
public void DavidsMethod
{
 
}
}
}
 
Then your line would need to be "DavidWimbley.Application.App.DavidsClass" for your type
Jayanta Chatterjee at 14-Jan-13 10:08am
   
My Class Name is -: Form1
Jayanta Chatterjee at 14-Jan-13 10:11am
   
sorry ...
 
my Namespace is -: ForPrintPre
David_Wimbley at 14-Jan-13 10:16am
   
So your line should be Type type=Type.GetType("ForPrintPre.Form1");
Jayanta Chatterjee at 14-Jan-13 10:24am
   
Now the error move to here -:
 
method.Invoke(obj, null);//Object reference not set to an instance of an object.
Jayanta Chatterjee at 14-Jan-13 11:04am
   
object obj = this.GetType().InvokeMember(textBox1.Text, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, null);
 
I found this code ,but at the runtime i getting error that "Method 'ForPrintPre.Form1.CallMe()' not found."
 
My Namespace is -:ForPrintPre.
Class Name is -: Form1.
 
Please Help Me ......
David_Wimbley at 14-Jan-13 11:05am
   
You need to make sure your CallMe method is public, not private...right now according to what you posted above your CallMe method is private.
Jayanta Chatterjee at 14-Jan-13 11:12am
   
I change the access specifier from private to public ..
still I'm getting same error .... :-(
Jayanta Chatterjee at 14-Jan-13 11:17am
   
My code is -:
 
using System;
using System.Windows.Forms;
using System.Reflection;
 
namespace ForPrintPre
{
public partial class dynamicMethodCall : Form
{
public dynamicMethodCall()
{
InitializeComponent();
}
 
private void button1_Click(object sender, EventArgs e)
{
object obj = this.GetType().InvokeMember(textBox1.Text, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, null);
}
public void CallMe()
{
MessageBox.Show("In Call Me Method.");
}
}
}
David_Wimbley at 14-Jan-13 11:18am
   
Ok ill take a look give me a bit
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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 .
  Permalink  
Comments
Jayanta Chatterjee at 22-Jan-13 7:24am
   
I want its totaly depend on users input(Dynamically) invoked function...
rizwan muhammed khan gouri at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 460
1 Peter Leow 260
2 OriginalGriff 245
3 Maciej Los 175
4 John C Rayan 146
0 Sergey Alexandrovich Kryukov 7,203
1 OriginalGriff 5,587
2 Peter Leow 4,097
3 Mika Wendelius 2,850
4 CHill60 2,818


Advertise | Privacy | Mobile
Web04 | 2.8.150224.1 | Last Updated 19 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100