vs-mef: Non-generic ExportProvider.GetExports(Type) method missing

Given a Type variable, how can I get its export?

public object Resolve<T>() => exportProvider.GetExportedValue<T>();          // OK
public object Resolve(Type type) => exportProvider.GetExportedValue(type);   // ??

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (8 by maintainers)

Commits related to this issue

Most upvoted comments

If you can elaborate on why this is important for you

I cannot speak for the OP, but I had the same requirement a few years ago when I had to implement IServiceProvider through a composition container. I don’t remember how I solved it back then, but I did get it to work via vs-mef at some point. These days I don’t see much usage of IServiceProvider so unfortunately I don’t have this trick in my codebase anymore.

I think if you expose GetContractName publically that should be enough, I think you can just pass object into the generic parameter of e.g. GetExportedValue together with the contract name, something like that already seems to work (unless there is a hidden edge case where it fails?):

public static class ContainerExtensions
{
    private abstract class PartDiscoveryHelper : PartDiscovery
    {
        private PartDiscoveryHelper(Resolver resolver) : base(resolver) { }
        public static string GetContractNameHelper(Type type) => GetContractName(type);
    }

    public static object GetExportedValue(this ExportProvider container, Type type)
    {
        return container.GetExportedValue<object>(PartDiscoveryHelper.GetContractNameHelper(type));
    }
}