Click here to Skip to main content
15,913,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to know, if in the .net have a class to call a function/method in run time...

e.g:

Enter with a method name to call: sum(10,20)


The code sample, of my Ideia is something like this:

C#
class Program
{
    static void sum(int a, int b)
    {
        Console.WriteLine("{0}", a + b);
    }

    static void Main(string[] args)
    {
        Console.Write("Enter with a method name to call: ");
        string a = Convert.ToString(Console.ReadLine());

        // SAMPLE OF IDEA
        ProcessMacro PrM = new ProcessMacro(a);
    }
}


So, It's possible to do this ?
If yes, how I can do this ?

Thanks.
Posted

You can use reflection:

System.Reflection.MemberInfo[] members = this.GetType().GetMembers();
var parms = new object[0];
Type TypeToReflect = Type.GetType("System.Int32");
var EmitObj = System.Activator.CreateInstance(this.GetType(),false);
var x = this.GetType().InvokeMember("TestMethod2", BindingFlags.InvokeMethod, null, EmitObj, parms);


I am not an expert in Reflection, and there may be a way to execute on an existing object.
 
Share this answer
 
This was asked in the past hour. You use the CodeDom namespace to do this.
 
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