/// <summary> /// Extracts the ImportDefinition for the T in an ExportFactory{T} import, if applicable. /// </summary> /// <param name="definition">The ImportDefinition which may be an ExportFactory.</param> /// <returns>The import definition that describes the created part, or <c>null</c> if the import definition isn't an ExportFactory.</returns> private static MefV1.Primitives.ImportDefinition GetExportFactoryProductImportDefinitionIfApplicable(MefV1.Primitives.ImportDefinition definition) { // The optimal path that we can code for at the moment is using the internal interface. if (IPartCreatorImportDefinition_MightFail != null && ProductImportDefinition_MightFail != null) { if (IPartCreatorImportDefinition_MightFail.IsInstanceOfType(definition)) { return((MefV1.Primitives.ImportDefinition)ProductImportDefinition_MightFail.GetValue(definition)); } } else { // The internal interface, or its member, is gone. Fallback to using the public API that throws. try { if (ReflectionModelServices.IsExportFactoryImportDefinition(definition)) { return(ReflectionModelServices.GetExportFactoryProductImportDefinition(definition)); } } catch (ArgumentException) { // MEFv1 throws rather than simply returning false when the ImportDefinition is of the incorrect type. } } return(null); }
public SerializableImportDefinition(ImportDefinition importDefinition) { IsExportFactory = ReflectionModelServices.IsExportFactoryImportDefinition(importDefinition); if (IsExportFactory) { // Handle export factories. importDefinition = ReflectionModelServices.GetExportFactoryProductImportDefinition(importDefinition); } ContractName = importDefinition.ContractName; Cardinality = importDefinition.Cardinality; IsRecomposable = importDefinition.IsRecomposable; IsPrerequisite = importDefinition.IsPrerequisite; Metadata = new SerializableDictionary <string, object>(importDefinition.Metadata); ImportingMember = ReflectionModelServices.IsImportingParameter(importDefinition) ? new SerializableLazyMemberInfo(ReflectionModelServices.GetImportingParameter(importDefinition).Value) : new SerializableLazyMemberInfo(ReflectionModelServices.GetImportingMember(importDefinition)); var contractBasedImportDefinition = importDefinition as ContractBasedImportDefinition; if (contractBasedImportDefinition != null) { RequiredTypeIdentity = contractBasedImportDefinition.RequiredTypeIdentity; RequiredCreationPolicy = contractBasedImportDefinition.RequiredCreationPolicy; RequiredMetadata = new SerializableDictionary <string, string>(contractBasedImportDefinition.RequiredMetadata.Select(kvp => new KeyValuePair <string, string>(kvp.Key, kvp.Value.AssemblyQualifiedName))); } }
/// <summary> /// Extracts the ImportDefinition for the T in an ExportFactory{T} import, if applicable. /// </summary> /// <param name="definition">The ImportDefinition which may be an ExportFactory.</param> /// <returns>The import definition that describes the created part, or <c>null</c> if the import definition isn't an ExportFactory.</returns> private static MefV1.Primitives.ImportDefinition GetExportFactoryProductImportDefinitionIfApplicable(MefV1.Primitives.ImportDefinition definition) { #if NET45 // The optimal path that we can code for at the moment is using the internal interface. if (IPartCreatorImportDefinition_MightFail != null && ProductImportDefinition_MightFail != null) { if (IPartCreatorImportDefinition_MightFail.IsInstanceOfType(definition)) { return((MefV1.Primitives.ImportDefinition)ProductImportDefinition_MightFail.GetValue(definition)); } } else { // The internal interface, or its member, is gone. Fallback to using the public API that throws. try { if (ReflectionModelServices.IsExportFactoryImportDefinition(definition)) { return(ReflectionModelServices.GetExportFactoryProductImportDefinition(definition)); } } catch (ArgumentException) { // In .NET 4.5, ReflectionModelServices.IsExportFactoryImportDefinition throws // rather than simply returning false when the ImportDefinition is of the incorrect type. // This was fixed in .NET 4.6 with this bug: // Bug 1005218: ReflectionModelServices.IsExportFactoryImportDefinition should not throw ArgumentException } } return(null); #else return(ReflectionModelServices.IsExportFactoryImportDefinition(definition) ? ReflectionModelServices.GetExportFactoryProductImportDefinition(definition) : null); #endif }
/// <summary> /// Extracts the ImportDefinition for the T in an ExportFactory{T} import, if applicable. /// </summary> /// <param name="definition">The ImportDefinition which may be an ExportFactory.</param> /// <returns>The import definition that describes the created part, or <c>null</c> if the import definition isn't an ExportFactory.</returns> private static MefV1.Primitives.ImportDefinition GetExportFactoryProductImportDefinitionIfApplicable(MefV1.Primitives.ImportDefinition definition) { return(ReflectionModelServices.IsExportFactoryImportDefinition(definition) ? ReflectionModelServices.GetExportFactoryProductImportDefinition(definition) : null); }