Calling a function from a string in C#

前端 未结 6 1621
别那么骄傲
别那么骄傲 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: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();
            }
        }
    

提交回复
热议问题