怎样实现把函数名(常量)当成函数执行
比如我有一个函数,Do_Task(int ID),现在数据库里存有Do_Task(2),Do_Task(3),现在读数据库并执行Do_Task(2),Do_Task(3)
[解决办法]
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;namespace ConsoleApplication1{ class A { public void DoTask(int id) { Console.WriteLine("dotask: " + id); } public void DoAnotherTask(int id) { Console.WriteLine("other: " + id); } } class Program { static void Main(string[] args) { string s1 = "DoTask(2)"; string s2 = "DoAnotherTask(100)"; ExeMethod(s1); ExeMethod(s2); } static void ExeMethod(string exp) { string methodname = Regex.Match(exp, @"\w+(?=\()").Value; int param = int.Parse(Regex.Match(exp, @"(?<=\()\d+(?=\))").Value); typeof(A).GetMethods().First(x => x.Name == methodname).Invoke(new A(), new object[] { param }); } }}
[解决办法]