dimanche 30 août 2015

Assembly.GetReferencedAssemblies method and portable class library

I think am getting bold trying to understand why in portable class library, which targets .net 4.5 and silverlight 5 and has support for MEF, some MEF and Reflection stuff is missing. For example: Assembly.GetReferencedAssemblies() doesn't exists in portable class library. DirectoryContainer from MEF doesn't exists. Problem is, i need it really bad. I have portable class library and ServiceLocator class in it.

public class ServiceLocator
{
    private Dictionary<Type, string> _defaultServices;

    [ImportMany(typeof(IService), AllowRecomposition = true, RequiredCreationPolicy = CreationPolicy.Shared)]
    private IEnumerable<Lazy<object, IService>> SharedServices { get; set; }

    [ImportMany(typeof(IService), AllowRecomposition = true, RequiredCreationPolicy = CreationPolicy.NonShared)]
    private IEnumerable<Lazy<object, IService>> NonSharedServices { get; set; }

    public ServiceLocator()
    {
        _defaultServices = new Dictionary<Type, string>();

        var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
        var cat = new AssemblyCatalog
        var container = new CompositionContainer(catalog);

        container.ComposeParts(this);

        foreach (var service in SharedServices)
        {
            if (service.Metadata.IsDefault)
            {
                SetDefaultService(service.Metadata.ServiceName, service.Metadata.ServiceInterface);
            }
        }
    }

Then, i have platform specific class library that references portable one, has class that implements IService interface from portable class library and exports it. Finally, i have WPF application that reference platform specific class library. The problem is, exported service is not in WPF application, but in referenced platform specific class library, so var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); can't import service. That is why i need DirectoryCatalog or Assembly.GetReferencedAssemblies(). Is there some workaround?





Aucun commentaire:

Enregistrer un commentaire