当前位置: 代码迷 >> ASP.NET >> 怎么实现把函数名(常量)当成函数执行
  详细解决方案

怎么实现把函数名(常量)当成函数执行

热度:9405   发布时间:2013-02-25 00:00:00.0
怎样实现把函数名(常量)当成函数执行
比如我有一个函数,Do_Task(int ID),现在数据库里存有Do_Task(2),Do_Task(3),现在读数据库并执行Do_Task(2),Do_Task(3)

------解决方案--------------------------------------------------------
C# code
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 });                    }    }}
------解决方案--------------------------------------------------------
探讨

引用:

C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class A
……
  相关解决方案