Пример #1
0
        public void GetOpenGenericExport(IContainer container)
        {
            var v3Container = (TestUtilities.V3ContainerWrapper)container;

            var v1Container = new MefV1.Hosting.CompositionContainer(v3Container.ExportProvider.AsExportProvider());
            var tree        = v1Container.GetExportedValue <Tree <int> >();

            Assert.NotNull(tree);
        }
Пример #2
0
        public static PluginHost Create()
        {
            var builder = new System.ComponentModel.Composition.Registration.RegistrationBuilder();

            // addins
            builder
                .ForTypesDerivedFrom<IAddinContract>()
                //.Export<IAddinContract>()
                .ExportInterfaces()
                ;

            // host
            builder
                    .ForType<PluginHost>()
                    //.ImportProperty(x => x.Addins, b => b.AsMany())
                    .Export<PluginHost>()
                    ;

            var hostCatalog = new System.ComponentModel.Composition.Hosting.AssemblyCatalog(typeof(PluginHost).Assembly, builder);
            var aCataog = new System.ComponentModel.Composition.Hosting.AggregateCatalog(hostCatalog);

            #if false
            var catalog = new System.ComponentModel.Composition.Hosting.DirectoryCatalog("addins");
            aCataog.Catalogs.Add(catalog);
            #else
            foreach (var f in new DirectoryInfo("addins").GetFiles().Where(x => x.Name.ToLower().EndsWith(".dll")))
            {
                var catalog = new System.ComponentModel.Composition.Hosting.AssemblyCatalog(Assembly.LoadFile(f.FullName), builder);
                aCataog.Catalogs.Add(catalog);
            }
            #endif

            var container = new System.ComponentModel.Composition.Hosting.CompositionContainer(aCataog);
            #if false
            var host = new PluginHost();
            container.ComposeParts(host);
            return host;
            #else
            return container.GetExportedValue<PluginHost>();
            #endif
        }
Пример #3
0
        static public PluginHost Create()
        {
            var builder = new System.ComponentModel.Composition.Registration.RegistrationBuilder();

            // addins
            builder
            .ForTypesDerivedFrom <IAddinContract>()
            //.Export<IAddinContract>()
            .ExportInterfaces()
            ;

            // host
            builder
            .ForType <PluginHost>()
            //.ImportProperty(x => x.Addins, b => b.AsMany())
            .Export <PluginHost>()
            ;

            var hostCatalog = new System.ComponentModel.Composition.Hosting.AssemblyCatalog(typeof(PluginHost).Assembly, builder);
            var aCataog     = new System.ComponentModel.Composition.Hosting.AggregateCatalog(hostCatalog);

#if false
            var catalog = new System.ComponentModel.Composition.Hosting.DirectoryCatalog("addins");
            aCataog.Catalogs.Add(catalog);
#else
            foreach (var f in new DirectoryInfo("addins").GetFiles().Where(x => x.Name.ToLower().EndsWith(".dll")))
            {
                var catalog = new System.ComponentModel.Composition.Hosting.AssemblyCatalog(Assembly.LoadFile(f.FullName), builder);
                aCataog.Catalogs.Add(catalog);
            }
#endif

            var container = new System.ComponentModel.Composition.Hosting.CompositionContainer(aCataog);
#if false
            var host = new PluginHost();
            container.ComposeParts(host);
            return(host);
#else
            return(container.GetExportedValue <PluginHost>());
#endif
        }
Пример #4
0
        private static void Main(string[] args)
        {
            #region MEF1

            System.ComponentModel.Composition.Hosting.AggregateCatalog aggregateCatalog = new System.ComponentModel.Composition.Hosting.AggregateCatalog();
            aggregateCatalog.Catalogs.Add(new System.ComponentModel.Composition.Hosting.AssemblyCatalog(typeof(Program).Assembly));
            System.ComponentModel.Composition.Hosting.CompositionContainer compositionContainer = new System.ComponentModel.Composition.Hosting.CompositionContainer(aggregateCatalog);

            {
                ClassA classA = compositionContainer.GetExportedValue <ClassA>();
                classA.Log();
                classA.ClassB.Value.Log();
                classA.ClassB.Value.Log();
            }

            {
                ClassA classA = compositionContainer.GetExportedValue <ClassA>();
                classA.Log();
                classA.ClassB.Value.Log();
                classA.ClassB.Value.Log();
            }

            {
                ClassB classB = compositionContainer.GetExportedValue <ClassB>();
                classB.Log();
                classB.ClassA.Value.Log();
                classB.ClassA.Value.Log();
            }

            {
                ClassB classB = compositionContainer.GetExportedValue <ClassB>();
                classB.Log();
                classB.ClassA.Value.Log();
                classB.ClassA.Value.Log();
            }

            #endregion

            #region MEF2

            System.Composition.Hosting.ContainerConfiguration containerConfiguration = new System.Composition.Hosting.ContainerConfiguration().WithAssembly(typeof(Program).Assembly);
            System.Composition.Hosting.CompositionHost        compositionHost        = containerConfiguration.CreateContainer();

            {
                ClassA classA = compositionHost.GetExport <ClassA>();
                classA.Log();
                classA.ClassB.Value.Log();
                classA.ClassB.Value.Log();
            }

            {
                ClassA classA = compositionHost.GetExport <ClassA>();
                classA.Log();
                classA.ClassB.Value.Log();
                classA.ClassB.Value.Log();
            }

            {
                ClassB classB = compositionHost.GetExport <ClassB>();
                classB.Log();
                classB.ClassA.Value.Log();
                classB.ClassA.Value.Log();
            }

            {
                ClassB classB = compositionHost.GetExport <ClassB>();
                classB.Log();
                classB.ClassA.Value.Log();
                classB.ClassA.Value.Log();
            }

            #endregion
        }