文字列のリストが提供される状況があります。リストの最初のエントリはメソッドの名前です。リストの残りの文字列はメソッド引数です。タスクを使用してメソッドを実行したいと思います(教育目的で)。メソッド名をタスク命令に入力できる適切な手順を理解するのに問題があります。
この例では、タスクとして実行できる2つの静的メソッドがあります。args[1]は私の選択を意味します。
public class Program
{
private static ILog log = LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
string whichPrint = args[1];
Type type = typeof(Program);
MethodInfo meth = type.GetMethod(whichPrint, BindingFlags.Public |
BindingFlags.Static);
//this is the problem area....how do I feed the the method or delegate into
//the lambda expression ????
Delegate methDel = meth.CreateDelegate(type);
Task t = Task.Factory.StartNew(() => methDel("help!!"));
}
static void printme1(string s)
{
log.Debug("Printme1 Printing: " + s);
}
static void printme2(string s)
{
log.Debug("Printme2 Printing: " + s);
}
}
methDelが変数として表示されているため、コンパイルできません。方法として見るには、それか何か他のものが必要です。タスクに割り当てることができるメソッドがたくさんある可能性があるため、case/switchステートメントは使用しません。