MEF: GetExportedValue из Типа? - PullRequest
       6

MEF: GetExportedValue из Типа?

3 голосов
/ 05 января 2011

Используя MEF, я могу создать и загрузить тип, подобный этому:

var view = Container.GetExportedValue<MyView>();

Теперь я хочу сделать следующее:

Type t = typeof(MyView);
var view = Container.GetExportedValue<t>();

(конечно, тип может содержать что-тоотличается от MyView).

Это невозможно при использовании универсальных выражений GetExportedValue <> - есть ли другой способ добиться этого?

Ответы [ 2 ]

8 голосов
/ 05 января 2011

Вы можете использовать отражение.
Вот пример:

using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Reflection;

namespace WindowsFormsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            AggregateCatalog catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(IMessage).Assembly));
            CompositionContainer container = new CompositionContainer(catalog);

            Type t = typeof(IMessage);
            var m = container.GetExportedValue(t);
        }
    }

    public static class CompositionContainerExtension
    {
        public static object GetExportedValue(this ExportProvider container, Type type)
        {
            // get a reference to the GetExportedValue<T> method
            MethodInfo methodInfo = container.GetType().GetMethods()
                                      .Where(d => d.Name == "GetExportedValue"
                                                  && d.GetParameters().Length == 0).First();

            // create an array of the generic types that the GetExportedValue<T> method expects
            Type[] genericTypeArray = new Type[] { type };

            // add the generic types to the method
            methodInfo = methodInfo.MakeGenericMethod(genericTypeArray);

            // invoke GetExportedValue<type>()
            return methodInfo.Invoke(container, null);
        }
    }

    public interface IMessage
    {
        string Message { get; }
    }

    [Export(typeof(IMessage))]
    public class MyMessage : IMessage
    {
        public string Message
        {
            get { return "test"; }
        }
    }
}
5 голосов
/ 15 июня 2011
...