开发者

How do you call a delegate exported via MEF with late binding?

What I'm trying to do is for this to work:

GetMethod(Key key)(...some arguments...)

The "...some arguments..." varies between each call of GetMethod, including in number of parameters, so there is no specific Action<...> that I can use.

Now the GetMethod looks through the argument list to find a match within a list of Tuple and returns the second argument of the Tuple, which is the dynamic. This list was created using MEF by

<some CompositionContainer>.GetExports(new ImportDefinition(a => true, "...", ImportCardinality.ZeroOrMore, true, false))
            .Select(e => (Tuple<Key, dynamic>)Tuple.Create((Key)e.Metadata["Key"], e.Value))
            .ToList();

Now, it seems that the second item in the typle isn't actually an Action of the appropriate type, but rather a ExportedDelegate. Is there any way to make so that the second item is actually a correct Act开发者_运维技巧ion, so that GetMethod(...)(...) actually does work?


Call ExportedDelegate.CreateDelegate. Pass in the type of delegate you want (ie Action). MEF uses ExportedDelegate so that you aren't restricted to using Action<> and Func<> delegates, but can use any delegate type with a matching signature.

You also probably need to specify the contract name in the constraint you pass to the ImportDefinition constructor, instead of just passing a => true.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜