-
Notifications
You must be signed in to change notification settings - Fork 87
/
ImportDefinitionBinding.cs
221 lines (184 loc) · 8.51 KB
/
ImportDefinitionBinding.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.VisualStudio.Composition
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Composition.Reflection;
public class ImportDefinitionBinding : IEquatable<ImportDefinitionBinding>
{
private bool? isLazy;
private TypeRef importingSiteElementTypeRef;
/// <summary>
/// Initializes a new instance of the <see cref="ImportDefinitionBinding"/> class
/// to represent an importing member.
/// </summary>
public ImportDefinitionBinding(
ImportDefinition importDefinition,
TypeRef composablePartType,
MemberRef importingMember,
TypeRef importingSiteTypeRef,
TypeRef importingSiteTypeWithoutCollectionRef)
{
Requires.NotNull(importDefinition, nameof(importDefinition));
Requires.NotNull(composablePartType, nameof(composablePartType));
Requires.NotNull(importingSiteTypeRef, nameof(importingSiteTypeRef));
Requires.NotNull(importingSiteTypeWithoutCollectionRef, nameof(importingSiteTypeWithoutCollectionRef));
this.ImportDefinition = importDefinition;
this.ComposablePartTypeRef = composablePartType;
this.ImportingMemberRef = importingMember;
this.ImportingSiteTypeRef = importingSiteTypeRef;
this.ImportingSiteTypeWithoutCollectionRef = importingSiteTypeWithoutCollectionRef;
}
/// <summary>
/// Initializes a new instance of the <see cref="ImportDefinitionBinding"/> class
/// to represent a parameter in an importing constructor.
/// </summary>
public ImportDefinitionBinding(
ImportDefinition importDefinition,
TypeRef composablePartType,
ParameterRef importingConstructorParameter,
TypeRef importingSiteTypeRef,
TypeRef importingSiteTypeWithoutCollectionRef)
{
Requires.NotNull(importDefinition, nameof(importDefinition));
Requires.NotNull(composablePartType, nameof(composablePartType));
Requires.NotNull(importingSiteTypeRef, nameof(importingSiteTypeRef));
Requires.NotNull(importingSiteTypeWithoutCollectionRef, nameof(importingSiteTypeWithoutCollectionRef));
this.ImportDefinition = importDefinition;
this.ComposablePartTypeRef = composablePartType;
this.ImportingParameterRef = importingConstructorParameter;
this.ImportingSiteTypeRef = importingSiteTypeRef;
this.ImportingSiteTypeWithoutCollectionRef = importingSiteTypeWithoutCollectionRef;
}
/// <summary>
/// Gets the definition for this import.
/// </summary>
public ImportDefinition ImportDefinition { get; private set; }
/// <summary>
/// Gets the member this import is found on. Null for importing constructors.
/// </summary>
public MemberInfo ImportingMember => this.ImportingMemberRef?.MemberInfo;
/// <summary>
/// Gets the member this import is found on. Null for importing constructors.
/// </summary>
public MemberRef ImportingMemberRef { get; private set; }
public ParameterInfo ImportingParameter => this.ImportingParameterRef?.ParameterInfo;
public ParameterRef ImportingParameterRef { get; private set; }
public Type ComposablePartType => this.ComposablePartTypeRef?.ResolvedType;
public TypeRef ComposablePartTypeRef { get; private set; }
/// <summary>
/// Gets the actual type of the variable or member that will be assigned the result.
/// This includes any Lazy, ExportFactory or collection wrappers.
/// </summary>
/// <value>Never null.</value>
public Type ImportingSiteType => this.ImportingSiteTypeRef?.Resolve();
/// <summary>
/// Gets the actual type of the variable or member that will be assigned the result.
/// This includes any Lazy, ExportFactory or collection wrappers.
/// </summary>
/// <value>Never null.</value>
public TypeRef ImportingSiteTypeRef { get; }
public TypeRef ImportingSiteTypeWithoutCollectionRef { get; }
public Type ImportingSiteTypeWithoutCollection => this.ImportingSiteTypeWithoutCollectionRef?.ResolvedType;
/// <summary>
/// Gets the type of the member, with the ImportMany collection and Lazy/ExportFactory stripped off, when present.
/// </summary>
public TypeRef ImportingSiteElementTypeRef
{
get
{
if (this.importingSiteElementTypeRef == null)
{
this.importingSiteElementTypeRef = PartDiscovery.GetTypeIdentityFromImportingTypeRef(this.ImportingSiteTypeRef, this.ImportDefinition.Cardinality == ImportCardinality.ZeroOrMore);
}
return this.importingSiteElementTypeRef;
}
}
/// <summary>
/// Gets the type of the member, with the ImportMany collection and Lazy/ExportFactory stripped off, when present.
/// </summary>
public Type ImportingSiteElementType => this.ImportingSiteElementTypeRef?.Resolve();
public bool IsLazy
{
get
{
if (!this.isLazy.HasValue)
{
this.isLazy = this.ImportingSiteTypeWithoutCollectionRef.IsAnyLazyType();
}
return this.isLazy.Value;
}
}
public Type MetadataType
{
get
{
if (this.IsLazy || this.IsExportFactory)
{
var args = this.ImportingSiteTypeWithoutCollectionRef.GenericTypeArguments;
if (args.Length == 2)
{
return args[1].ResolvedType;
}
}
return null;
}
}
public bool IsExportFactory
{
get { return this.ImportingSiteTypeWithoutCollectionRef.IsExportFactoryType(); }
}
public Type ExportFactoryType
{
get { return this.IsExportFactory ? this.ImportingSiteTypeWithoutCollection : null; }
}
public override int GetHashCode()
{
return this.ImportDefinition.GetHashCode();
}
public override bool Equals(object obj)
{
return this.Equals(obj as ImportDefinitionBinding);
}
public bool Equals(ImportDefinitionBinding other)
{
if (other == null)
{
return false;
}
bool result = this.ImportDefinition.Equals(other.ImportDefinition)
&& EqualityComparer<TypeRef>.Default.Equals(this.ComposablePartTypeRef, other.ComposablePartTypeRef)
&& EqualityComparer<MemberRef>.Default.Equals(this.ImportingMemberRef, other.ImportingMemberRef)
&& EqualityComparer<ParameterRef>.Default.Equals(this.ImportingParameterRef, other.ImportingParameterRef);
return result;
}
public void ToString(TextWriter writer)
{
var indentingWriter = IndentingTextWriter.Get(writer);
indentingWriter.WriteLine("ImportDefinition:");
using (indentingWriter.Indent())
{
this.ImportDefinition.ToString(writer);
}
indentingWriter.WriteLine("ComposablePartType: {0}", this.ComposablePartType.FullName);
indentingWriter.WriteLine("ImportingMember: {0}", this.ImportingMember);
indentingWriter.WriteLine("ParameterInfo: {0}", this.ImportingParameter);
indentingWriter.WriteLine("ImportingSiteType: {0}", this.ImportingSiteType);
}
internal void GetInputAssemblies(ISet<AssemblyName> assemblies)
{
Requires.NotNull(assemblies, nameof(assemblies));
this.ImportDefinition.GetInputAssemblies(assemblies);
this.ComposablePartTypeRef.GetInputAssemblies(assemblies);
this.ImportingMemberRef?.GetInputAssemblies(assemblies);
this.ImportingParameterRef?.GetInputAssemblies(assemblies);
this.ImportingSiteTypeRef.GetInputAssemblies(assemblies);
this.ComposablePartTypeRef.GetInputAssemblies(assemblies);
}
}
}