/
TypeReference.cs
170 lines (145 loc) · 5.72 KB
/
TypeReference.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Reflection.Metadata.Ecma335;
namespace System.Reflection.Metadata
{
public readonly struct TypeReference
{
private readonly MetadataReader _reader;
// Workaround: JIT doesn't generate good code for nested structures, so use RowId.
private readonly uint _treatmentAndRowId;
internal TypeReference(MetadataReader reader, uint treatmentAndRowId)
{
Debug.Assert(reader != null);
Debug.Assert(treatmentAndRowId != 0);
_reader = reader;
_treatmentAndRowId = treatmentAndRowId;
}
private int RowId
{
get { return (int)(_treatmentAndRowId & TokenTypeIds.RIDMask); }
}
private TypeRefTreatment Treatment
{
get { return (TypeRefTreatment)(_treatmentAndRowId >> TokenTypeIds.RowIdBitCount); }
}
private TypeReferenceHandle Handle
{
get { return TypeReferenceHandle.FromRowId(RowId); }
}
/// <summary>
/// Resolution scope in which the target type is defined and is uniquely identified by the specified <see cref="Namespace"/> and <see cref="Name"/>.
/// </summary>
/// <remarks>
/// Resolution scope can be one of the following handles:
/// <list type="bullet">
/// <item><description><see cref="TypeReferenceHandle"/> of the enclosing type, if the target type is a nested type.</description></item>
/// <item><description><see cref="ModuleReferenceHandle"/>, if the target type is defined in another module within the same assembly as this one.</description></item>
/// <item><description><see cref="EntityHandle.ModuleDefinition"/>, if the target type is defined in the current module. This should not occur in a CLI compressed metadata module.</description></item>
/// <item><description><see cref="AssemblyReferenceHandle"/>, if the target type is defined in a different assembly from the current module.</description></item>
/// <item><description>Nil handle if the target type must be resolved by searching the <see cref="MetadataReader.ExportedTypes"/> for a matching <see cref="Namespace"/> and <see cref="Name"/>.</description></item>
/// </list>
/// </remarks>
public EntityHandle ResolutionScope
{
get
{
if (Treatment == 0)
{
return _reader.TypeRefTable.GetResolutionScope(Handle);
}
return GetProjectedResolutionScope();
}
}
/// <summary>
/// Name of the target type.
/// </summary>
public StringHandle Name
{
get
{
if (Treatment == 0)
{
return _reader.TypeRefTable.GetName(Handle);
}
return GetProjectedName();
}
}
/// <summary>
/// Full name of the namespace where the target type is defined, or nil if the type is nested or defined in a root namespace.
/// </summary>
public StringHandle Namespace
{
get
{
if (Treatment == 0)
{
return _reader.TypeRefTable.GetNamespace(Handle);
}
return GetProjectedNamespace();
}
}
#region Projections
private EntityHandle GetProjectedResolutionScope()
{
switch (Treatment)
{
case TypeRefTreatment.SystemAttribute:
case TypeRefTreatment.SystemDelegate:
return AssemblyReferenceHandle.FromVirtualIndex(AssemblyReferenceHandle.VirtualIndex.System_Runtime);
case TypeRefTreatment.UseProjectionInfo:
return MetadataReader.GetProjectedAssemblyRef(RowId);
}
Debug.Assert(false, "Unknown TypeRef treatment");
return default(AssemblyReferenceHandle);
}
private StringHandle GetProjectedName()
{
if (Treatment == TypeRefTreatment.UseProjectionInfo)
{
return MetadataReader.GetProjectedName(RowId);
}
else
{
return _reader.TypeRefTable.GetName(Handle);
}
}
private StringHandle GetProjectedNamespace()
{
switch (Treatment)
{
case TypeRefTreatment.SystemAttribute:
case TypeRefTreatment.SystemDelegate:
return StringHandle.FromVirtualIndex(StringHandle.VirtualIndex.System);
case TypeRefTreatment.UseProjectionInfo:
return MetadataReader.GetProjectedNamespace(RowId);
}
Debug.Assert(false, "Unknown TypeRef treatment");
return default(StringHandle);
}
internal TypeRefSignatureTreatment SignatureTreatment
{
get
{
if (Treatment == 0)
{
return TypeRefSignatureTreatment.None;
}
return GetProjectedSignatureTreatment();
}
}
private TypeRefSignatureTreatment GetProjectedSignatureTreatment()
{
if (Treatment == TypeRefTreatment.UseProjectionInfo)
{
return MetadataReader.GetProjectedSignatureTreatment(RowId);
}
else
{
return TypeRefSignatureTreatment.None;
}
}
#endregion
}
}