我在運行時加載一些程序集並使用Reflections(MethodInfo.Invoke)調用它們上的方法。異步運行時方法調用
現在我想讓這些調用異步。所以我想使用Delegate.BeginInvoke()。但我不確定如何通過在運行時提供函數名稱來創建委託實例。 (我看到的所有示例都有在編譯時本身解析委託實例目標。)我有一個包含要調用的方法的MethodInfo對象。有沒有辦法做到這一點?
public void Invocation(Object[] inputObjs)
{
public delegate string DelegateMethodInfo(int num);
Assembly assm = Assembly.Load(assemblyName);
Type type = assm.GetType(className);
Type[] ctorParams = new Type[0];
Object[] objs = new Object[0];
ConstructorInfo ctorInf = type.GetConstructor(ctorParams);
Object classObj = ctorInf.Invoke(objs);
MethodInfo methodInf = type.GetMethod(methodName);
// Need asynchronous invocation.
//Object retObj = methodInf.Invoke(classObj, inputObjs);
DelegateMethodInfo del = new DelegateMethodInfo(???); // How to instantiate the delegate???
del.BeginInvoke((int)inputObjs[0], null, null);
}
難道是準確的說是使用ThreadPool.QueueUserWorkItem在Delegate.CreateDelegate是「動異步執行更高調用堆棧」?無論如何,這對我來說似乎是一個更好的方法。 – 2009-11-04 19:49:21
這是一個相當合理的描述,是的。 – 2009-11-04 19:53:24