1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.ComponentModel.Composition.Primitives; 6 7 namespace System.ComponentModel.Composition.Factories 8 { 9 internal static partial class PartFactory 10 { GetAttributedExporterType()11 public static Type GetAttributedExporterType() 12 { 13 return typeof(ExportValueTypeFactory); 14 } 15 CreateAttributed(Type type)16 public static ComposablePart CreateAttributed(Type type) 17 { 18 var definition = PartDefinitionFactory.CreateAttributed(type); 19 20 return definition.CreatePart(); 21 } 22 CreateAttributed(object instance)23 public static ComposablePart CreateAttributed(object instance) 24 { 25 return AttributedModelServices.CreatePart(instance); 26 } 27 Create()28 public static ComposablePart Create() 29 { 30 return new NoOverridesComposablePart(); 31 } 32 CreateExporter(string contractName, object value)33 public static ComposablePart CreateExporter(string contractName, object value) 34 { 35 return CreateExporter(new MicroExport(contractName, value)); 36 } 37 CreateExporter(params MicroExport[] exports)38 public static ComposablePart CreateExporter(params MicroExport[] exports) 39 { 40 ConcreteComposablePart part = new ConcreteComposablePart(); 41 42 if (exports != null) 43 { 44 foreach (var export in exports) 45 { 46 foreach (object exportedValue in export.ExportedValues) 47 { 48 part.AddExport(ExportFactory.Create(export.ContractName, exportedValue, export.Metadata)); 49 } 50 } 51 } 52 53 return part; 54 } 55 CreateImporter()56 public static ImportingComposablePart CreateImporter<T>() 57 { 58 string contractName = AttributedModelServices.GetContractName(typeof(T)); 59 60 return CreateImporter(contractName); 61 } 62 CreateImporterExporter(string exportContractName, string importContractName)63 public static ImportingComposablePart CreateImporterExporter(string exportContractName, string importContractName) 64 { 65 return new ImportingComposablePart(exportContractName, ImportCardinality.ExactlyOne, false, importContractName); 66 } 67 CreateImporter(params string[] contractNames)68 public static ImportingComposablePart CreateImporter(params string[] contractNames) 69 { 70 return new ImportingComposablePart(ImportCardinality.ZeroOrMore, false, contractNames); 71 } 72 CreateImporter(bool isRecomposable, params string[] contractNames)73 public static ImportingComposablePart CreateImporter(bool isRecomposable, params string[] contractNames) 74 { 75 return new ImportingComposablePart(ImportCardinality.ZeroOrMore, isRecomposable, contractNames); 76 } 77 CreateImporter(string contractName)78 public static ImportingComposablePart CreateImporter(string contractName) 79 { 80 return CreateImporter(contractName, ImportCardinality.ZeroOrMore); 81 } 82 CreateImporter(string contractName, bool isRecomposable)83 public static ImportingComposablePart CreateImporter(string contractName, bool isRecomposable) 84 { 85 return CreateImporter(contractName, ImportCardinality.ZeroOrMore, isRecomposable); 86 } 87 CreateImporter(string contractName, ImportCardinality cardinality)88 public static ImportingComposablePart CreateImporter(string contractName, ImportCardinality cardinality) 89 { 90 return CreateImporter(contractName, cardinality, false); 91 } 92 CreateImporter(string contractName, ImportCardinality cardinality, bool isRecomposable)93 public static ImportingComposablePart CreateImporter(string contractName, ImportCardinality cardinality, bool isRecomposable) 94 { 95 return new ImportingComposablePart(cardinality, isRecomposable, contractName); 96 } 97 CreateImporter(params ImportDefinition[] importDefinitions)98 public static ImportingComposablePart CreateImporter(params ImportDefinition[] importDefinitions) 99 { 100 return new ImportingComposablePart(importDefinitions); 101 } 102 CreateDisposable(Action<bool> disposeCallback)103 public static ComposablePart CreateDisposable(Action<bool> disposeCallback) 104 { 105 return new DisposableComposablePart(disposeCallback); 106 } 107 } 108 } 109