Calling a function from a string in C#

前端 未结 6 1516
别那么骄傲
别那么骄傲 2020-11-22 11:41

I know in php you are able to make a call like:

$function_name = \'hello\';
$function_name();

function hello() { echo \'hello\'; }

Is this

相关标签:
6条回答
  • 2020-11-22 12:23
    This code works in my console .Net application
    class Program
    {
        static void Main(string[] args)
        {
            string method = args[0]; // get name method
            CallMethod(method);
        }
        
        public static void CallMethod(string method)
        {
            try
            {
                Type type = typeof(Program);
                MethodInfo methodInfo = type.GetMethod(method);
                methodInfo.Invoke(method, null);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                Console.ReadKey();
            }
        }
        
        public static void Hello()
        {
            string a = "hello world!";
            Console.WriteLine(a);
            Console.ReadKey();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 12:32
    class Program
        {
            static void Main(string[] args)
            {
                Type type = typeof(MyReflectionClass);
                MethodInfo method = type.GetMethod("MyMethod");
                MyReflectionClass c = new MyReflectionClass();
                string result = (string)method.Invoke(c, null);
                Console.WriteLine(result);
    
            }
        }
    
        public class MyReflectionClass
        {
            public string MyMethod()
            {
                return DateTime.Now.ToString();
            }
        }
    
    0 讨论(0)
  • 2020-11-22 12:35

    Yes. You can use reflection. Something like this:

    Type thisType = this.GetType();
    MethodInfo theMethod = thisType.GetMethod(TheCommandString);
    theMethod.Invoke(this, userParameters);
    
    0 讨论(0)
  • 2020-11-22 12:38

    You can invoke methods of a class instance using reflection, doing a dynamic method invocation:

    Suppose that you have a method called hello in a the actual instance (this):

    string methodName = "hello";
    
    //Get the method information using the method info class
     MethodInfo mi = this.GetType().GetMethod(methodName);
    
    //Invoke the method
    // (null- no parameter for the method call
    // or you can pass the array of parameters...)
    mi.Invoke(this, null);
    
    0 讨论(0)
  • 2020-11-22 12:38

    In C#, you can create delegates as function pointers. Check out the following MSDN article for information on usage: http://msdn.microsoft.com/en-us/library/ms173171(VS.80).aspx

        public static void hello()
        {
            Console.Write("hello world");
        }
    
       /* code snipped */
    
        public delegate void functionPointer();
    
        functionPointer foo = hello;
        foo();  // Writes hello world to the console.
    
    0 讨论(0)
  • 2020-11-22 12:41

    A slight tangent -- if you want to parse and evaluate an entire expression string which contains (nested!) functions, consider NCalc (http://ncalc.codeplex.com/ and nuget)

    Ex. slightly modified from the project documentation:

    // the expression to evaluate, e.g. from user input (like a calculator program, hint hint college students)
    var exprStr = "10 + MyFunction(3, 6)";
    Expression e = new Expression(exprString);
    
    // tell it how to handle your custom function
    e.EvaluateFunction += delegate(string name, FunctionArgs args) {
            if (name == "MyFunction")
                args.Result = (int)args.Parameters[0].Evaluate() + (int)args.Parameters[1].Evaluate();
        };
    
    // confirm it worked
    Debug.Assert(19 == e.Evaluate());
    

    And within the EvaluateFunction delegate you would call your existing function.

    0 讨论(0)
提交回复
热议问题