少し例を挙げてみましょう。
class Session (
public delegate string CleanBody();
public static void Execute(string name, string q, CleanBody body) ...
次のように使用できます。
Session.Execute("foo", "bar", delegate() { string x="beep"; /* whatever*/ return x; });
しかし、実行する必要がある場合は、 MethodInfo.Invoke を介して実行する必要があります-異なる dll のように、どちらの方法でも型の依存関係はありません。お気に入り:
Type type = Type.GetType("Bla.Session, FooSessionDll", true);
MethodInfo methodInfo = type.GetMethod("Execute");
Object [] args = { "foo", "bar", delegate() // Doesn't compile, now that ?
{
string x="beep"; /* whatever*/ return x;
}
methodInfo.Invoke("Trial Execution :-)", args);
どのようなトリック/キャストが適用されても、それが本物のデリゲートとして Execute に到達するようにする必要があります。実際のデリゲートは、より複雑な署名などを持っている場合があります。