/
ExportDefinition.cs
136 lines (125 loc) · 5.2 KB
/
ExportDefinition.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Internal;
namespace System.ComponentModel.Composition.Primitives
{
/// <summary>
/// Describes the contract that an <see cref="Export"/> object satisfies.
/// </summary>
public class ExportDefinition
{
// Unlike contract name, metadata has a sensible default; set it to an empty bag,
// so that derived definitions only need to override ContractName by default.
private readonly IDictionary<string, object?> _metadata = MetadataServices.EmptyMetadata;
private readonly string? _contractName;
/// <summary>
/// Initializes a new instance of the <see cref="ExportDefinition"/> class.
/// </summary>
/// <remarks>
/// <note type="inheritinfo">
/// Derived types calling this constructor must override <see cref="ContractName"/>
/// and optionally, <see cref="Metadata"/>. By default, <see cref="Metadata"/>
/// returns an empty, read-only dictionary.
/// </note>
/// </remarks>
protected ExportDefinition()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ExportDefinition"/> class with
/// the specified contract name and metadata.
/// </summary>
/// <param name="contractName">
/// A <see cref="string"/> containing the contract name of the
/// <see cref="ExportDefinition"/>.
/// </param>
/// <param name="metadata">
/// An <see cref="IDictionary{TKey, TValue}"/> containing the metadata of the
/// <see cref="ExportDefinition"/>; or <see langword="null"/> to set the
/// <see cref="Metadata"/> property to an empty, read-only
/// <see cref="IDictionary{TKey, TValue}"/>.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="contractName"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="contractName"/> is an empty string ("").
/// </exception>
public ExportDefinition(string contractName, IDictionary<string, object?>? metadata)
{
Requires.NotNullOrEmpty(contractName, nameof(contractName));
_contractName = contractName;
if (metadata != null)
{
_metadata = metadata.AsReadOnly();
}
}
/// <summary>
/// Gets the contract name of the export definition.
/// </summary>
/// <value>
/// A <see cref="string"/> containing the contract name of the
/// <see cref="ExportDefinition"/>.
/// </value>
/// <exception cref="NotImplementedException">
/// The property was not overridden by a derived class.
/// </exception>
/// <remarks>
/// <note type="inheritinfo">
/// Overriders of this property should never return <see langword="null"/>
/// or an empty string ("").
/// </note>
/// </remarks>
public virtual string ContractName
{
get
{
if (_contractName != null)
{
return _contractName;
}
throw ExceptionBuilder.CreateNotOverriddenByDerived("ContractName");
}
}
/// <summary>
/// Gets the metadata of the export definition.
/// </summary>
/// <value>
/// An <see cref="IDictionary{TKey, TValue}"/> containing the metadata of the
/// <see cref="ExportDefinition"/>. The default is an empty, read-only
/// <see cref="IDictionary{TKey, TValue}"/>.
/// </value>
/// <remarks>
/// <para>
/// <note type="inheritinfo">
/// Overriders of this property should return a read-only
/// <see cref="IDictionary{TKey, TValue}"/> object with a case-sensitive,
/// non-linguistic comparer, such as <see cref="StringComparer.Ordinal"/>,
/// and should never return <see langword="null"/>.
/// If the <see cref="ExportDefinition"/> does not contain metadata
/// return an empty <see cref="IDictionary{TKey, TValue}"/> instead.
/// </note>
/// </para>
/// </remarks>
public virtual IDictionary<string, object?> Metadata
{
get
{
Debug.Assert(_metadata != null);
return _metadata;
}
}
/// <summary>
/// Returns a string representation of the export definition.
/// </summary>
/// <returns>
/// A <see cref="string"/> containing the value of the <see cref="ContractName"/> property.
/// </returns>
public override string ToString()
{
return ContractName;
}
}
}