in src/Microsoft.VisualStudio.Composition/Configuration/AttributedPartDiscovery.cs [320:391]
private bool TryCreateImportDefinition(Type importingType, ICustomAttributeProvider member, ImmutableHashSet<IImportSatisfiabilityConstraint> importConstraints, [NotNullWhen(true)] out ImportDefinition? importDefinition)
{
Requires.NotNull(importingType, nameof(importingType));
Requires.NotNull(member, nameof(member));
var importAttribute = member.GetFirstAttribute<ImportAttribute>();
var importManyAttribute = member.GetFirstAttribute<ImportManyAttribute>();
// Importing constructors get implied attributes on their parameters.
if (importAttribute == null && importManyAttribute == null && member is ParameterInfo)
{
importAttribute = new ImportAttribute();
}
var sharingBoundaries = ImmutableHashSet.Create<string>();
var sharingBoundaryAttribute = member.GetFirstAttribute<SharingBoundaryAttribute>();
if (sharingBoundaryAttribute != null)
{
Verify.Operation(importingType.IsExportFactoryTypeV2(), Strings.IsExpectedOnlyOnImportsOfExportFactoryOfTV2, typeof(SharingBoundaryAttribute).Name, importingType.FullName);
sharingBoundaries = sharingBoundaries.Union(sharingBoundaryAttribute.SharingBoundaryNames);
}
if (member is PropertyInfo importingMember && importingMember.SetMethod == null)
{
// MEFv2 quietly ignores such importing members.
importDefinition = null;
return false;
}
if (importAttribute != null)
{
this.ThrowOnInvalidImportingMemberOrParameter(member, isImportMany: false);
Type contractType = GetTypeIdentityFromImportingType(importingType, importMany: false);
if (contractType.IsAnyLazyType() || contractType.IsExportFactoryTypeV2())
{
contractType = contractType.GetTypeInfo().GetGenericArguments()[0];
}
importConstraints = importConstraints
.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),
importConstraints,
sharingBoundaries);
return true;
}
else if (importManyAttribute != null)
{
this.ThrowOnInvalidImportingMemberOrParameter(member, isImportMany: true);
Type contractType = GetTypeIdentityFromImportingType(importingType, importMany: true);
importConstraints = importConstraints
.Union(this.GetMetadataViewConstraints(importingType, importMany: true))
.Union(GetExportTypeIdentityConstraints(contractType));
importDefinition = new ImportDefinition(
string.IsNullOrEmpty(importManyAttribute.ContractName) ? GetContractName(contractType) : importManyAttribute.ContractName,
ImportCardinality.ZeroOrMore,
GetImportMetadataForGenericTypeImport(contractType),
importConstraints,
sharingBoundaries);
return true;
}
else
{
importDefinition = null;
return false;
}
}