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 export.
14 [SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes")]
15 [AttributeUsage(AttributeTargets
.Property
| AttributeTargets
.Field
| AttributeTargets
.Parameter
,
16 AllowMultiple
= false, Inherited
= false)]
17 public class ImportAttribute
: Attribute
, IAttributedImport
20 /// Initializes a new instance of the <see cref="ImportAttribute"/> class, importing the
21 /// export with the default contract name.
25 /// The default contract name is the result of calling
26 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on the property, field,
27 /// 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 ImportAttribute()
40 /// Initializes a new instance of the <see cref="ImportAttribute"/> class, importing the
41 /// export 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 export 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 ImportAttribute(Type contractType
)
64 : this((string)null, contractType
)
69 /// Initializes a new instance of the <see cref="ImportAttribute"/> class, importing the
70 /// export with the specified contract name.
72 /// <param name="contractName">
73 /// A <see cref="String"/> containing the contract name of the export 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 ImportAttribute(string contractName
)
88 : this(contractName
, (Type
)null)
92 public ImportAttribute(string contractName
, Type contractType
)
94 this.ContractName
= contractName
;
95 this.ContractType
= contractType
;
99 /// Gets the contract name of the export to import.
102 /// A <see cref="String"/> containing the contract name of the export 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, field or parameter will be set
120 /// to its type's default value when an export with the contract name is not present in
124 /// <see langword="true"/> if the property, field or parameter will be set
125 /// its type's default value when an export with the <see cref="ContractName"/> is not
126 /// present in the <see cref="CompositionContainer"/>; otherwise, <see langword="false"/>.
127 /// The default value is <see langword="false"/>.
131 /// The default value of a property's, field's or parameter's type is
132 /// <see langword="null"/> for reference types and 0 for numeric value types. For
133 /// other value types, the default value will be each field of the value type
134 /// initialized to zero, if the field is a value type or <see langword="null"/> if
135 /// the field is a reference type.
138 public bool AllowDefault { get; set; }
141 /// Gets or sets a value indicating whether the property or field will be recomposed
142 /// when exports that provide the same contract that this import expects, have changed
143 /// in the container.
146 /// <see langword="true"/> if the property or field allows for recomposition when exports
147 /// that provide the same <see cref="ContractName"/> are added or removed from the
148 /// <see cref="CompositionContainer"/>; otherwise, <see langword="false"/>.
149 /// The default value is <see langword="false"/>.
151 public bool AllowRecomposition { get; set; }
154 /// Gets or sets a value indicating that the importer requires a specific
155 /// <see cref="CreationPolicy"/> for the exports used to satisfy this import. T
158 /// <see cref="CreationPolicy.Any"/> - default value, used if the importer doesn't
159 /// require a specific <see cref="CreationPolicy"/>.
161 /// <see cref="CreationPolicy.Shared"/> - Requires that all exports used should be shared
162 /// by everyone in the container.
164 /// <see cref="CreationPolicy.NonShared"/> - Requires that all exports used should be
165 /// non-shared in a container and thus everyone gets their own instance.
167 public CreationPolicy RequiredCreationPolicy { get; set; }
169 ImportCardinality IAttributedImport
.Cardinality
173 if (this.AllowDefault
== true)
175 return ImportCardinality
.ZeroOrOne
;
177 return ImportCardinality
.ExactlyOne
;