internal void BuildAttributes(Type type, ref List <Attribute> attributes) { Attribute importAttribute; // Infer from Type when not explicitly set. bool asMany = (!_asManySpecified) ? type != s_stringType && typeof(IEnumerable).IsAssignableFrom(type) : _asMany; if (!asMany) { importAttribute = new ImportAttribute(_contractName, _contractType) { AllowDefault = _allowDefault, AllowRecomposition = _allowRecomposition, RequiredCreationPolicy = _requiredCreationPolicy, Source = _source }; } else { importAttribute = new ImportManyAttribute(_contractName, _contractType) { AllowRecomposition = _allowRecomposition, RequiredCreationPolicy = _requiredCreationPolicy, Source = _source }; } if (attributes == null) { attributes = new List <Attribute>(); } attributes.Add(importAttribute); }
public void AsMany_And_ContractName_ChangesGeneratedAttributeToImportMany() { var builder = new ConventionBuilder(); builder.ForType <FooImpl>().ImportProperty((p) => p.IFooProperty, (c) => c.AsContractName("hey").AsMany()); ImportManyAttribute importAtt = GetImportManyAttribute(builder); Assert.NotNull(importAtt); Assert.Equal("hey", importAtt.ContractName); }
private void PopulateList([NotNull] IList list, [NotNull] ImportManyAttribute attribute, [NotNull] RouteValueDictionary parameters) { Debug.ArgumentNotNull(list, nameof(list)); Debug.ArgumentNotNull(attribute, nameof(attribute)); Debug.ArgumentNotNull(parameters, nameof(parameters)); foreach (var item in catalog.Where(export => MatchesImport(export, attribute)).OrderBy(e => e.Attribute.Priority).ThenBy(e => e.Attribute.SortName).Select(e => e.GetInstance(parameters))) { list.Add(item); } }
public void ComposeImportMany(PropertyInfo p, Object target, ImportManyAttribute att) { if (p != null && target != null && att != null) { var r = from part in _catalog.Parts from d in part.ExportDefinitions.Where(k => k.Key == att.ContractType).Select(k => k.Value) from i in d select i; var createmethod = this.GetType().GetMethod("CreateItemsArray", BindingFlags.NonPublic | BindingFlags.Instance); createmethod.MakeGenericMethod(att.ContractType).Invoke(this, new object[] { p, target, r.AsEnumerable() }); } }
internal void BuildAttributes(Type type, ref List <Attribute> attributes) { Attribute importAttribute; var contractName = (_getContractNameFromPartType != null) ? _getContractNameFromPartType(type) : _contractName; // Infer from Type when not explicitly set. var asMany = _asMany ?? IsSupportedImportManyType(type.GetTypeInfo()); if (!asMany) { importAttribute = new ImportAttribute(contractName) { AllowDefault = _allowDefault }; } else { importAttribute = new ImportManyAttribute(contractName); } if (attributes == null) { attributes = new List <Attribute>(); } attributes.Add(importAttribute); //Add metadata attributes from direct specification if (_metadataConstraintItems != null) { foreach (Tuple <string, object> item in _metadataConstraintItems) { attributes.Add(new ImportMetadataConstraintAttribute(item.Item1, item.Item2)); } } //Add metadata attributes from func specification if (_metadataConstraintItemFuncs != null) { foreach (Tuple <string, Func <Type, object> > item in _metadataConstraintItemFuncs) { var name = item.Item1; var value = (item.Item2 != null) ? item.Item2(type) : null; attributes.Add(new ImportMetadataConstraintAttribute(name, value)); } } return; }
private bool MatchesImport([NotNull] ExportDefinition exportDefinition, [NotNull] ImportManyAttribute importManyAttribute) { Debug.ArgumentNotNull(exportDefinition, nameof(exportDefinition)); Debug.ArgumentNotNull(importManyAttribute, nameof(importManyAttribute)); if (importManyAttribute.ContractType != null && importManyAttribute.ContractType != exportDefinition.Attribute.ContractType) { return(false); } if (importManyAttribute.ContractName != null && importManyAttribute.ContractName != exportDefinition.Attribute.ContractName) { return(false); } return(true); }
private bool TryCreateImportDefinition(Type importingType, ICustomAttributeProvider member, [NotNullWhen(true)] out ImportDefinition?importDefinition) { Requires.NotNull(importingType, nameof(importingType)); Requires.NotNull(member, nameof(member)); ImportAttribute importAttribute = member.GetFirstAttribute <ImportAttribute>(); ImportManyAttribute importManyAttribute = member.GetFirstAttribute <ImportManyAttribute>(); // Importing constructors get implied attributes on their parameters. if (importAttribute == null && importManyAttribute == null && member is ParameterInfo) { importAttribute = new ImportAttribute(); } if (importAttribute != null) { this.ThrowOnInvalidImportingMemberOrParameter(member, isImportMany: false); if (importAttribute.Source != ImportSource.Any) { throw new NotSupportedException(Strings.CustomImportSourceNotSupported); } var requiredCreationPolicy = importingType.IsExportFactoryTypeV1() ? CreationPolicy.NonShared : (CreationPolicy)importAttribute.RequiredCreationPolicy; Type contractType = importAttribute.ContractType ?? GetTypeIdentityFromImportingType(importingType, importMany: false); var constraints = PartCreationPolicyConstraint.GetRequiredCreationPolicyConstraints(requiredCreationPolicy) .Union(this.GetMetadataViewConstraints(importingType, importMany: false)) .Union(GetExportTypeIdentityConstraints(contractType)); importDefinition = new ImportDefinition( string.IsNullOrEmpty(importAttribute.ContractName) ? GetContractName(contractType) : importAttribute.ContractName, importAttribute.AllowDefault ? ImportCardinality.OneOrZero : ImportCardinality.ExactlyOne, GetImportMetadataForGenericTypeImport(contractType), constraints); return(true); } else if (importManyAttribute != null) { this.ThrowOnInvalidImportingMemberOrParameter(member, isImportMany: true); if (importManyAttribute.Source != ImportSource.Any) { throw new NotSupportedException(Strings.CustomImportSourceNotSupported); } var requiredCreationPolicy = GetElementTypeFromMany(importingType).IsExportFactoryTypeV1() ? CreationPolicy.NonShared : (CreationPolicy)importManyAttribute.RequiredCreationPolicy; Type contractType = importManyAttribute.ContractType ?? GetTypeIdentityFromImportingType(importingType, importMany: true); var constraints = PartCreationPolicyConstraint.GetRequiredCreationPolicyConstraints(requiredCreationPolicy) .Union(this.GetMetadataViewConstraints(importingType, importMany: true)) .Union(GetExportTypeIdentityConstraints(contractType)); importDefinition = new ImportDefinition( string.IsNullOrEmpty(importManyAttribute.ContractName) ? GetContractName(contractType) : importManyAttribute.ContractName, ImportCardinality.ZeroOrMore, GetImportMetadataForGenericTypeImport(contractType), constraints); return(true); } else { importDefinition = null; return(false); } }
public void Ctor_ContractName(string contractName) { var attribute = new ImportManyAttribute(contractName); Assert.Equal(contractName, attribute.ContractName); }
public void Ctor_Default() { var attribute = new ImportManyAttribute(); Assert.Null(attribute.ContractName); }