我正在使用MEF
. 我调用这个方法来获取所有导出的方法:
var methods = container.GetExports<Func<int,int,double>,MyMetadata>("contract name");
注意:有时会Func<int,int,double>
发生变化。例如,它可能有更多参数:Func<int,int,int,double>
.
当我触发方法时,我知道要传递多少个参数。我的问题是如何动态地将参数传递给导入的方法?
更新:
IEnumerable<Lazy<Func<int,int,double>,MyMetadata>> Plugins;
public IEnumerable RunAllPlugins(int a, int b)
{
//my old approach
foreach (var p in Plugins)
{
dynamic a = p;
var b = a.Value.Invoke(a,b); //here I want to pass parameters dynamically
}
//this is new approach which is answer
//now I can pass parameters like array of objects
foreach(var lazyMethod in Plugins)
{
Delegate d=lazyMethod.Value;
object[] numbers=new object[]{1,2};
var result=d.DynamicInvoke(numbers);
}
return null;
}