-
Notifications
You must be signed in to change notification settings - Fork 87
/
ExportDefinitionBinding.cs
117 lines (101 loc) · 4.36 KB
/
ExportDefinitionBinding.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
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.VisualStudio.Composition
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Composition.Reflection;
public class ExportDefinitionBinding : IEquatable<ExportDefinitionBinding>
{
public ExportDefinitionBinding(ExportDefinition exportDefinition, ComposablePartDefinition partDefinition, MemberRef exportingMemberRef)
{
Requires.NotNull(exportDefinition, nameof(exportDefinition));
Requires.NotNull(partDefinition, nameof(partDefinition));
this.ExportDefinition = exportDefinition;
this.PartDefinition = partDefinition;
this.ExportingMemberRef = exportingMemberRef;
}
public ExportDefinition ExportDefinition { get; private set; }
// TODO: remove this member, perhaps in favor of just a property of type TypeRef,
// so that ComposablePartDefinition can contain a collection of ExportDefinitionBinding
// instead of just ExportDefinition in a dictionary.
// This would make it parallel to ImportDefinitionBinding.
public ComposablePartDefinition PartDefinition { get; private set; }
/// <summary>
/// Gets the member with the ExportAttribute applied. <c>null</c> when the export is on the type itself.
/// </summary>
public MemberInfo ExportingMember
{
get { return this.ExportingMemberRef?.MemberInfo; }
}
/// <summary>
/// Gets the member with the ExportAttribute applied. The return value is <c>null</c>
/// when the export is on the type itself.
/// </summary>
public MemberRef ExportingMemberRef { get; private set; }
/// <summary>
/// Gets a value indicating whether the exporting member is static.
/// </summary>
public bool IsStaticExport
{
get { return this.ExportingMemberRef.IsStatic(); }
}
public TypeRef ExportedValueTypeRef
{
get
{
if (this.ExportingMemberRef == null)
{
return this.PartDefinition.TypeRef;
}
else
{
return ReflectionHelpers.GetExportedValueTypeRef(this.PartDefinition.TypeRef, this.ExportingMemberRef);
}
}
}
public Type ExportedValueType
{
get { return this.ExportedValueTypeRef.ResolvedType; }
}
internal ExportDefinitionBinding CloseGenericExport(Type[] genericTypeArguments)
{
Requires.NotNull(genericTypeArguments, nameof(genericTypeArguments));
string exportTypeIdentity = string.Format(
CultureInfo.InvariantCulture,
(string)this.ExportDefinition.Metadata[CompositionConstants.ExportTypeIdentityMetadataName],
genericTypeArguments.Select(ContractNameServices.GetTypeIdentity).ToArray());
var updatedMetadata = ImmutableDictionary.CreateRange(this.ExportDefinition.Metadata)
.SetItem(CompositionConstants.ExportTypeIdentityMetadataName, exportTypeIdentity);
return new ExportDefinitionBinding(
new ExportDefinition(this.ExportDefinition.ContractName, updatedMetadata),
this.PartDefinition,
this.ExportingMemberRef);
}
public override bool Equals(object obj)
{
return this.Equals(obj as ExportDefinitionBinding);
}
public override int GetHashCode()
{
int hashCode = this.PartDefinition.TypeRef.GetHashCode();
if (this.ExportingMemberRef != null)
{
hashCode += this.ExportingMemberRef.GetHashCode();
}
return hashCode;
}
public bool Equals(ExportDefinitionBinding other)
{
bool result = this.PartDefinition.TypeRef.Equals(other.PartDefinition.TypeRef)
&& this.ExportDefinition.Equals(other.ExportDefinition)
&& this.ExportingMemberRef.Equals(other.ExportingMemberRef);
return result;
}
}
}