private ImportDefinition CreateWrapped(ContractBasedImportDefinition import, Type type) { if (ReflectionModelServices.IsImportingParameter(import)) { return(ReflectionModelServices.CreateImportDefinition( this.CreateWrapped(ReflectionModelServices.GetImportingParameter(import), type), import.ContractName, import.RequiredTypeIdentity, import.RequiredMetadata, import.Cardinality, import.RequiredCreationPolicy, null)); } else { return(ReflectionModelServices.CreateImportDefinition( this.CreateWrapped(ReflectionModelServices.GetImportingMember(import), type), import.ContractName, import.RequiredTypeIdentity, import.RequiredMetadata, import.Cardinality, import.IsRecomposable, import.RequiredCreationPolicy, 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))); } }
public static IDictionary <string, object> WriteImportDefinition(ComposablePartDefinition owner, ContractBasedImportDefinition importDefinition) { Assumes.NotNull(owner); Assumes.NotNull(importDefinition); Lazy <Type> partType = ReflectionModelServices.GetPartType(owner); IDictionary <string, object> cache = new Dictionary <string, object>(); cache.WriteContractName(importDefinition.ContractName); cache.WriteValue(AttributedCacheServices.CacheKeys.RequiredTypeIdentity, importDefinition.RequiredTypeIdentity, null); cache.WriteRequiredMetadata(importDefinition.RequiredMetadata); cache.WriteValue(AttributedCacheServices.CacheKeys.RequiredCreationPolicy, importDefinition.RequiredCreationPolicy, CreationPolicy.Any); cache.WriteValue(AttributedCacheServices.CacheKeys.Cardinality, importDefinition.Cardinality, ImportCardinality.ExactlyOne); if (ReflectionModelServices.IsImportingParameter(importDefinition)) { cache.WriteValue(AttributedCacheServices.CacheKeys.ImportType, AttributedCacheServices.ImportTypes.Parameter); cache.WriteLazyParameter( ReflectionModelServices.GetImportingParameter(importDefinition), partType); } else { // don't write anything for import type - member assumed LazyMemberInfo importingMemberInfo = ReflectionModelServices.GetImportingMember(importDefinition); cache.WriteValue(AttributedCacheServices.CacheKeys.IsRecomposable, importDefinition.IsRecomposable, false); cache.WriteValue(AttributedCacheServices.CacheKeys.MemberType, importingMemberInfo.MemberType, MemberTypes.Property); cache.WriteLazyAccessors( importingMemberInfo.GetAccessors(), partType); } return(cache); }
private static SerializableImportDefinition CreatePropertyImport( ContractBasedImportDefinition import, Func <Type, TypeIdentity> identityGenerator) { var memberInfo = ReflectionModelServices.GetImportingMember(import); if (memberInfo.MemberType != MemberTypes.Property) { throw new ArgumentOutOfRangeException("import"); } // this is really ugly because we assume that the underlying methods for a property are named as: // get_PROPERTYNAME and set_PROPERTYNAME. In this case we assume that imports always // have a set method. var getMember = memberInfo.GetAccessors().First(m => m.Name.Contains("set_")); var name = getMember.Name.Substring("set_".Length); var property = getMember.DeclaringType.GetProperty(name); var requiredType = ExtractRequiredType(property.GetCustomAttributes(), property.PropertyType); if (requiredType == null) { return(null); } return(PropertyBasedImportDefinition.CreateDefinition( import.ContractName, TypeIdentity.CreateDefinition(requiredType), import.Cardinality, import.IsRecomposable, import.RequiredCreationPolicy, property, identityGenerator)); }
public static Type GetMethodType(ImportDefinition definition, Type importDefinitionType) { var memberInfos = ReflectionModelServices.GetImportingMember(definition).GetAccessors(); var memberInfo = memberInfos[0]; if (memberInfo.MemberType == MemberTypes.Method) { var methodInfo = (MethodInfo)memberInfo; importDefinitionType = methodInfo.ReturnType; } else if (memberInfo.MemberType == MemberTypes.Field) { var fieldInfo = (FieldInfo)memberInfo; importDefinitionType = fieldInfo.FieldType; } return(importDefinitionType); }
/// <summary> /// Resolves the required part factory type for the given definition. /// </summary> /// <param name="definition">The import definition.</param> /// <returns>The resolved part factory type.</returns> /// <exception cref="NotSupportedException">If the import type is not supported.</exception> private static Type ResolvePartFactoryType(ContractBasedImportDefinition definition) { Type importType; var member = ReflectionModelServices.GetImportingMember(definition); var setter = member.GetAccessors() .Where(a => a.Name.StartsWith(SetterPrefix)) .OfType <MethodInfo>() .SingleOrDefault(); if (setter != null) { importType = setter.GetParameters() .First() .ParameterType; } else { importType = member.GetAccessors() .OfType <FieldInfo>() .Single() .FieldType; } if (definition.Cardinality == ImportCardinality.ZeroOrMore) { importType = importType.IsGenericType ? importType.GetGenericArguments()[0] : importType.GetElementType(); } if (!(importType.IsGenericType && (importType.GetGenericTypeDefinition() == typeof(PartFactory <>) || importType.GetGenericTypeDefinition() == typeof(PartFactory <,>)))) { Throw.Throw.NotSupported(String.Format(CultureInfo.CurrentUICulture, Resources.Exceptions.ImportTypeNotSupported, importType.FullName)); } return(importType); }
private static Type GetMethodType(ImportDefinition definition) { Type importDefinitionType = null; var member = ReflectionModelServices.GetImportingMember(definition); if (member.MemberType == MemberTypes.Property) { var methodInfo = (MethodInfo)member.GetAccessors()[1]; importDefinitionType = methodInfo.GetParameters()[0].ParameterType; } else if (member.MemberType == MemberTypes.Method) { var methodInfo = (MethodInfo)member.GetAccessors()[0]; importDefinitionType = methodInfo.ReturnType; } else if (member.MemberType == MemberTypes.Field) { var fieldInfo = (FieldInfo)member.GetAccessors()[0]; importDefinitionType = fieldInfo.FieldType; } return(importDefinitionType); }
private static void AddMemberType(ExportSource exportSource, ImportDefinition definition) { try { LazyMemberInfo member = ReflectionModelServices.GetImportingMember(definition); switch (member.MemberType) { case MemberTypes.Property: MemberInfo[] accessors = member.GetAccessors(); MethodInfo setter = accessors.OfType <MethodInfo>().Single(m => m.ReturnType == typeof(void)); Type type = setter.GetParameters()[0].ParameterType; exportSource.AddType(type); return; default: return; } } catch (ArgumentException) { // There is no TryGetImportingMember method, so if definition is of the wrong type, we just swallow exception } }
public static IEnumerable <Attribute> GetImportDefinitionAttributes(ImportDefinition definition) { if (ReflectionModelServices.IsImportingParameter(definition)) { var parameter = ReflectionModelServices.GetImportingParameter(definition); foreach (var attribute in Attribute.GetCustomAttributes(parameter.Value, true)) { yield return(attribute); } } else { var member = ReflectionModelServices.GetImportingMember(definition); MemberInfo attributedMember = null; switch (member.MemberType) { case MemberTypes.Property: attributedMember = FindProperty(member); break; case MemberTypes.Field: attributedMember = member.GetAccessors()[0]; break; } if (attributedMember != null) { foreach (var attribute in Attribute.GetCustomAttributes(attributedMember, true)) { yield return(attribute); } } } }
static Type GetTargetPartCreatorType(ContractBasedImportDefinition partCreatorImportDefinition) { Type importType = null; var member = ReflectionModelServices.GetImportingMember(partCreatorImportDefinition); if (member != null) { var setMi = member.GetAccessors() .Where(a => a.Name.StartsWith(PropertySetterPrefix)) .OfType <MethodInfo>() .SingleOrDefault(); if (setMi != null) { importType = setMi.GetParameters() .First() .ParameterType; } else { importType = member.GetAccessors() .OfType <FieldInfo>() .Single() .FieldType; } } else { var param = ReflectionModelServices.GetImportingParameter(partCreatorImportDefinition); if (param != null) { importType = param.Value.ParameterType; } else { throw new NotSupportedException("Import type not supported."); } } if (partCreatorImportDefinition.Cardinality == ImportCardinality.ZeroOrMore) { if (importType.IsGenericType) { importType = importType.GetGenericArguments()[0]; } else if (importType.IsArray) { importType = importType.GetElementType(); } } if (!(importType.IsGenericType && importType.GetGenericTypeDefinition() == typeof(ExportFactory <>) || importType.GetGenericTypeDefinition() == typeof(ExportFactory <,>))) { throw new NotSupportedException("Import type not supported."); } return(importType); }
protected override IEnumerable <Export> GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition) { Export export; if (!FExports.TryGetValue(definition, out export)) { var contractName = definition.ContractName; if (contractName == typeof(IIOFactory).FullName) { export = new Export(contractName, () => FIOFactory); } else { var typeToExport = ExportProviderUtils.GetImportDefinitionType(definition); foreach (var attribute in ExportProviderUtils.GetImportDefinitionAttributes(definition)) { var ioAttribute = attribute as IOAttribute; if (ioAttribute == null) { continue; } if (!ReflectionModelServices.IsImportingParameter(definition)) { var member = ReflectionModelServices.GetImportingMember(definition); if (member.MemberType == MemberTypes.Property) { foreach (var accessor in member.GetAccessors()) { if (FAccessorToExportMap.TryGetValue(accessor, out export)) { break; } } } } if (export == null) { var context = IOBuildContext.Create(typeToExport, ioAttribute); if (FIOFactory.CanCreateIOContainer(context)) { export = new Export(contractName, () => FIOFactory.CreateIO(context)); // Now register the export for all the base members if (!ReflectionModelServices.IsImportingParameter(definition)) { var member = ReflectionModelServices.GetImportingMember(definition); if (member.MemberType == MemberTypes.Property) { foreach (var accessor in member.GetAccessors().OfType <MethodInfo>()) { RegisterExport(accessor, export); } } } } } if (export != null) { break; } } } FExports.Add(definition, export); } if (export != null) { yield return(export); } }
/// <summary> /// Gets all the exports that match the constraint defined by the specified definition. /// </summary> /// <param name="definition">The object that defines the conditions of the /// System.ComponentModel.Composition.Primitives.Export objects to return.</param> /// <param name="atomicComposition">The transactional container for the composition.</param> /// <returns>A collection that contains all the exports that match the specified condition.</returns> protected override IEnumerable <Export> GetExportsCore( ImportDefinition definition, AtomicComposition atomicComposition) { var contractName = definition.ContractName; var nothingToDo = string.IsNullOrEmpty(contractName) || // no contract (definition.Cardinality != ImportCardinality.ZeroOrOne && definition.Cardinality != ImportCardinality.ExactlyOne); // we only support single value cardinalities if (nothingToDo) { yield break; } if (this.configurationSource.ContainsSetting(contractName)) { // import was found to be an app setting - may need to convert it to an appropriate type for the importer Type targetType = null; string stringValue = null; if (ReflectionModelServices.IsImportingParameter(definition)) { // import appears on a parameter var importingParameter = ReflectionModelServices.GetImportingParameter(definition); targetType = importingParameter.Value.ParameterType; stringValue = this.configurationSource.GetSetting(contractName); } else { // import appears on a member (property) var getAccessor = ReflectionModelServices .GetImportingMember(definition) .GetAccessors() .Where(x => x is MethodInfo) .Select(x => x as MethodInfo) .FirstOrDefault(x => (x.Attributes & MethodAttributes.SpecialName) == MethodAttributes.SpecialName && x.Name.StartsWith("get_", StringComparison.Ordinal)); if (getAccessor == null) { // cannot determine type of importing property yield break; } targetType = getAccessor.ReturnType; stringValue = this.configurationSource.GetSetting(contractName); } if (targetType == null) { yield break; } var export = new Export(contractName, () => Convert.ChangeType(stringValue, targetType)); yield return(export); } else if (this.configurationSource.ContainsSection(contractName)) { // import was found to be a configuration section var section = this.configurationSource.GetSection(contractName); yield return(new Export(contractName, () => section)); } }