1 // -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
5 using System
.ComponentModel
.Composition
.Hosting
;
6 using System
.ComponentModel
.Composition
.Primitives
;
7 using System
.Diagnostics
.CodeAnalysis
;
9 namespace System
.ComponentModel
.Composition
12 /// Specifies that a property, field, or parameter imports a particular set of exports.
14 [SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes")]
15 [AttributeUsage(AttributeTargets
.Property
| AttributeTargets
.Field
| AttributeTargets
.Parameter
,
16 AllowMultiple
= false, Inherited
= false)]
17 public class ImportManyAttribute
: Attribute
, IAttributedImport
20 /// Initializes a new instance of the <see cref="ImportManyAttribute"/> class, importing the
21 /// set of exports with the default contract name.
25 /// The default contract name is the result of calling
26 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on the element\item type of
27 /// theproperty, field, or parameter type that this is marked with this attribute.
30 /// The contract name is compared using a case-sensitive, non-linguistic comparison
31 /// using <see cref="StringComparer.Ordinal"/>.
34 public ImportManyAttribute()
40 /// Initializes a new instance of the <see cref="ImportManyAttribute"/> class, importing the
41 /// set of exports with the contract name derived from the specified type.
43 /// <param name="contractType">
44 /// A <see cref="Type"/> of which to derive the contract name of the exports to import, or
45 /// <see langword="null"/> to use the default contract name.
49 /// The contract name is the result of calling
50 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on
51 /// <paramref name="contractType"/>.
54 /// The default contract name is the result of calling
55 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on the property, field,
56 /// or parameter type that is marked with this attribute.
59 /// The contract name is compared using a case-sensitive, non-linguistic comparison
60 /// using <see cref="StringComparer.Ordinal"/>.
63 public ImportManyAttribute(Type contractType
)
64 : this((string)null, contractType
)
69 /// Initializes a new instance of the <see cref="ImportManyAttribute"/> class, importing the
70 /// set of exports with the specified contract name.
72 /// <param name="contractName">
73 /// A <see cref="String"/> containing the contract name of the exports to import, or
74 /// <see langword="null"/> or an empty string ("") to use the default contract name.
78 /// The default contract name is the result of calling
79 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on the property, field,
80 /// or parameter type that is marked with this attribute.
83 /// The contract name is compared using a case-sensitive, non-linguistic comparison
84 /// using <see cref="StringComparer.Ordinal"/>.
87 public ImportManyAttribute(string contractName
)
88 : this(contractName
, (Type
)null)
92 public ImportManyAttribute(string contractName
, Type contractType
)
94 this.ContractName
= contractName
;
95 this.ContractType
= contractType
;
99 /// Gets the contract name of the exports to import.
102 /// A <see cref="String"/> containing the contract name of the exports to import. The
103 /// default value is an empty string ("").
105 public string ContractName { get; private set; }
108 /// Get the contract type of the export to import.
111 /// A <see cref="Type"/> of the export that this import is expecting. The default value is
112 /// <see langword="null"/> which means that the type will be obtained by looking at the type on
113 /// the member that this import is attached to. If the type is <see cref="object"/> then the
114 /// importer is delaring they can accept any exported type.
116 public Type ContractType { get; private set; }
119 /// Gets or sets a value indicating whether the property or field will be recomposed
120 /// when exports that provide the same contract that this import expects, have changed
121 /// in the container.
124 /// <see langword="true"/> if the property or field allows for recomposition when exports
125 /// that provide the same <see cref="ContractName"/> are added or removed from the
126 /// <see cref="CompositionContainer"/>; otherwise, <see langword="false"/>.
127 /// The default value is <see langword="false"/>.
129 public bool AllowRecomposition { get; set; }
132 /// Gets or sets a value indicating that the importer requires a specific
133 /// <see cref="CreationPolicy"/> for the exports used to satisfy this import. T
136 /// <see cref="CreationPolicy.Any"/> - default value, used if the importer doesn't
137 /// require a specific <see cref="CreationPolicy"/>.
139 /// <see cref="CreationPolicy.Shared"/> - Requires that all exports used should be shared
140 /// by everyone in the container.
142 /// <see cref="CreationPolicy.NonShared"/> - Requires that all exports used should be
143 /// non-shared in a container and thus everyone gets their own instance.
145 public CreationPolicy RequiredCreationPolicy { get; set; }
147 ImportCardinality IAttributedImport
.Cardinality
149 get { return ImportCardinality.ZeroOrMore; }