Click here to Skip to main content
13,054,779 members (70,211 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have one function is created in the following
public void ExecStringToFunction(int pn)
        case 1:
            frmform1 frm1=new frmform1();
        case 2:
            frmform2 frm2=new frmform2();
private void butExec_Click(object sender, EventArgs e)
    int ni=1;
    string strExec="ExecStringToFunction("+ ni.ToString()+")";      

how to execute this function this string variable ???

Please send me email alerts for: [Email Removed]
Posted 6-Oct-12 5:04am
Updated 6-Oct-12 6:35am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You want to execute the code in the string variable?? No, that's bad. If you think you need to do that, you've got a serious problem with your design.

Why do you even have this code structured like this?? From what you've posted, there is no need to do this what-so-ever.
BillWoodruff 7-Oct-12 0:44am
Spot-on, Dave.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Dave K., in his solution above, is absolutely correct: you don't want to execute the contents of a string as code unless you just can't avoid it: it's a big pain in the butt; however, that does not mean it cannot be done.

In practice, there are two ways to approach this in C#, one through using System.CodeDom to create/compile/run code in memory from a string, and the other way is to use Reflection.Emit, and build code, piece by bloody piece, from parsing your string.

This CP article shows you an example of the CodeDom technique: [^]. Another, older, CP article: [^]. And another: [^].
Rate this: bad
Please Sign up or sign in to vote.

Solution 4


As way to work with such stuff you just can use reflection.

<pre>// We using  that 
using System.Reflection;
int ni = 1;
// we are call local method
Type _type = this.GetType();
// look for method we interested in
MethodInfo _method = _type.GetMethod("ExecStringToFunction");
// Execute that method
if (_method != null) _method.Invoke(this, new object[] { ni });

Here I look the function in local space and execute it with the parameter.
You just need to make the parser or use some of existing one which will parse function name and arguments. Ah also don't forget abt permissions.

For all debators who wonder what for that functionality can be necessary, and it not design or some other things. For example: I did huge library which can be extended with runtime scripts which were compiled at runtime. So I use C# or VB.NET as a script for extending library functionality. Example 3: I load assembly manually and check for method at runtime not as a referenced assembly - this for .NET plugins development (this way also you can call a managed exe methods); Example 3: have class with the huge structure with a variable number of properties and can be any number of substructures as property value - here is also better to use reflection to extract property name as type and call the method based on it.

Also, for debators, the question was asked correctly, why are you making discussion from that place?
If you don't know how to make it just do not post the solution. Or you just gaining your reputation via posting answers without any information or making comments that "your question is not correct" or something like that? Maybe you just start reading the books to give the good answers for others?


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 7 Oct 2012
Copyright © CodeProject, 1999-2017
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