Click here to Skip to main content
15,909,498 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one function is created in the following
C#
public void ExecStringToFunction(int pn)
{
    switch(pn)
    {
        case 1:
            frmform1 frm1=new frmform1();
            frm1.Showdialog();
            break;
        case 2:
            frmform2 frm2=new frmform2();
            frm2.Showdialog();
        default:
            break;
    }
}

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
Updated 6-Oct-12 6:35am
v3

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.
 
Share this answer
 
Comments
BillWoodruff 7-Oct-12 0:44am    
Spot-on, Dave.
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: [^].
 
Share this answer
 
Hello,

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

C#
<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?

Regards,
Maxim.
 
Share this answer
 

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