日期:2014-05-17 浏览次数:20544 次
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 });
}
}
}
------解决方案--------------------