我在运行时加载一些程序集并使用反射(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);
}