Skip to content

Commit 1c3b1b5

Browse files
Add GetDeclaringType to PropertyDefinition and EventDefinition. (#111646)
* Add `GetDeclaringType` to `PropertyDefinition` and `EventDefinition`. * Add test. * Add documentation to all `GetDeclaringType` APIs. * Remove unnecessary code. * Update tests. We assert that the test assembly has types with events and properties, and acknowledge that it does not have nested types. --------- Co-authored-by: Jan Kotas <jkotas@microsoft.com>
1 parent 34ec4f5 commit 1c3b1b5

File tree

12 files changed

+136
-1
lines changed

12 files changed

+136
-1
lines changed

src/libraries/System.Reflection.Metadata/ref/System.Reflection.Metadata.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ public readonly partial struct EventDefinition
715715
public System.Reflection.Metadata.EntityHandle Type { get { throw null; } }
716716
public System.Reflection.Metadata.EventAccessors GetAccessors() { throw null; }
717717
public System.Reflection.Metadata.CustomAttributeHandleCollection GetCustomAttributes() { throw null; }
718+
public System.Reflection.Metadata.TypeDefinitionHandle GetDeclaringType() { throw null; }
718719
}
719720
public readonly partial struct EventDefinitionHandle : System.IEquatable<System.Reflection.Metadata.EventDefinitionHandle>
720721
{
@@ -2135,6 +2136,7 @@ public readonly partial struct PropertyDefinition
21352136
public System.Reflection.Metadata.MethodSignature<TType> DecodeSignature<TType, TGenericContext>(System.Reflection.Metadata.ISignatureTypeProvider<TType, TGenericContext> provider, TGenericContext genericContext) { throw null; }
21362137
public System.Reflection.Metadata.PropertyAccessors GetAccessors() { throw null; }
21372138
public System.Reflection.Metadata.CustomAttributeHandleCollection GetCustomAttributes() { throw null; }
2139+
public System.Reflection.Metadata.TypeDefinitionHandle GetDeclaringType() { throw null; }
21382140
public System.Reflection.Metadata.ConstantHandle GetDefaultValue() { throw null; }
21392141
}
21402142
public readonly partial struct PropertyDefinitionHandle : System.IEquatable<System.Reflection.Metadata.PropertyDefinitionHandle>

src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/Internal/Tables.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,29 @@ internal int GetEventListStartFor(int rowId)
12131213
int rowOffset = (rowId - 1) * this.RowSize;
12141214
return this.Block.PeekReference(rowOffset + _EventListOffset, _IsEventRefSizeSmall);
12151215
}
1216+
1217+
internal TypeDefinitionHandle FindTypeContainingEvent(int eventRowId, int numberOfEvents)
1218+
{
1219+
int numOfRows = this.NumberOfRows;
1220+
int slot = this.Block.BinarySearchForSlot(numOfRows, this.RowSize, _EventListOffset, (uint)eventRowId, _IsEventRefSizeSmall);
1221+
int row = slot + 1;
1222+
if (row == 0)
1223+
{
1224+
return default(TypeDefinitionHandle);
1225+
}
1226+
1227+
if (row > numOfRows)
1228+
{
1229+
if (eventRowId <= numberOfEvents)
1230+
{
1231+
return GetParentType(numOfRows);
1232+
}
1233+
1234+
return default(TypeDefinitionHandle);
1235+
}
1236+
1237+
return GetParentType(row);
1238+
}
12161239
}
12171240

12181241
internal readonly struct EventPtrTableReader
@@ -1343,6 +1366,29 @@ internal int GetPropertyListStartFor(int rowId)
13431366
int rowOffset = (rowId - 1) * this.RowSize;
13441367
return this.Block.PeekReference(rowOffset + _PropertyListOffset, _IsPropertyRefSizeSmall);
13451368
}
1369+
1370+
internal TypeDefinitionHandle FindTypeContainingProperty(int propertyRowId, int numberOfProperties)
1371+
{
1372+
int numOfRows = this.NumberOfRows;
1373+
int slot = this.Block.BinarySearchForSlot(numOfRows, this.RowSize, _PropertyListOffset, (uint)propertyRowId, _IsPropertyRefSizeSmall);
1374+
int row = slot + 1;
1375+
if (row == 0)
1376+
{
1377+
return default(TypeDefinitionHandle);
1378+
}
1379+
1380+
if (row > numOfRows)
1381+
{
1382+
if (propertyRowId <= numberOfProperties)
1383+
{
1384+
return GetParentType(numOfRows);
1385+
}
1386+
1387+
return default(TypeDefinitionHandle);
1388+
}
1389+
1390+
return GetParentType(row);
1391+
}
13461392
}
13471393

13481394
internal readonly struct PropertyPtrTableReader

src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/MetadataReader.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,26 @@ internal TypeDefinitionHandle GetDeclaringType(FieldDefinitionHandle fieldDef)
13501350
return TypeDefTable.FindTypeContainingField(fieldRowId, FieldTable.NumberOfRows);
13511351
}
13521352

1353+
internal TypeDefinitionHandle GetDeclaringType(EventDefinitionHandle eventDef)
1354+
{
1355+
if (UseEventPtrTable)
1356+
{
1357+
eventDef = EventPtrTable.GetEventFor(eventDef.RowId);
1358+
}
1359+
1360+
return EventMapTable.FindTypeContainingEvent(eventDef.RowId, EventTable.NumberOfRows);
1361+
}
1362+
1363+
internal TypeDefinitionHandle GetDeclaringType(PropertyDefinitionHandle propertyDef)
1364+
{
1365+
if (UsePropertyPtrTable)
1366+
{
1367+
propertyDef = PropertyPtrTable.GetPropertyFor(propertyDef.RowId);
1368+
}
1369+
1370+
return PropertyMapTable.FindTypeContainingProperty(propertyDef.RowId, PropertyTable.NumberOfRows);
1371+
}
1372+
13531373
public string GetString(DocumentNameBlobHandle handle)
13541374
{
13551375
return BlobHeap.GetDocumentName(handle);

src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/TypeSystem/EventDefinition.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ public CustomAttributeHandleCollection GetCustomAttributes()
5757
return new CustomAttributeHandleCollection(_reader, Handle);
5858
}
5959

60+
/// <summary>
61+
/// Returns a handle to the type that declares this event.
62+
/// </summary>
63+
public TypeDefinitionHandle GetDeclaringType()
64+
{
65+
return _reader.GetDeclaringType(Handle);
66+
}
67+
6068
public EventAccessors GetAccessors()
6169
{
6270
int adder = 0;

src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/TypeSystem/FieldDefinition.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public TType DecodeSignature<TType, TGenericContext>(ISignatureTypeProvider<TTyp
8383
return decoder.DecodeFieldSignature(ref blob);
8484
}
8585

86+
/// <summary>
87+
/// Returns a handle to the type that declares this field.
88+
/// </summary>
8689
public TypeDefinitionHandle GetDeclaringType()
8790
{
8891
return _reader.GetDeclaringType(Handle);

src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/TypeSystem/MethodDefinition.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ public MethodImplAttributes ImplAttributes
109109
}
110110
}
111111

112+
/// <summary>
113+
/// Returns a handle to the type that declares this method.
114+
/// </summary>
112115
public TypeDefinitionHandle GetDeclaringType()
113116
{
114117
return _reader.GetDeclaringType(Handle);

src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/TypeSystem/PropertyDefinition.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ public CustomAttributeHandleCollection GetCustomAttributes()
6969
return new CustomAttributeHandleCollection(_reader, Handle);
7070
}
7171

72+
/// <summary>
73+
/// Returns a handle to the type that declares this property.
74+
/// </summary>
75+
public TypeDefinitionHandle GetDeclaringType()
76+
{
77+
return _reader.GetDeclaringType(Handle);
78+
}
79+
7280
public PropertyAccessors GetAccessors()
7381
{
7482
int getter = 0;

src/libraries/System.Reflection.Metadata/tests/Metadata/MetadataReaderTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,7 @@ public void MemberCollections_AllMembers()
27232723
"add_ED1",
27242724
"remove_ED1",
27252725
".ctor",
2726+
".ctor",
27262727
"get_PE1",
27272728
"set_PE1",
27282729
"get_PE2",

src/libraries/System.Reflection.Metadata/tests/Metadata/TypeSystem/TypeDefinitionTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Reflection.Metadata.Ecma335;
45
using Xunit;
56

67
namespace System.Reflection.Metadata.Tests
@@ -32,5 +33,44 @@ public void ValidateTypeDefinitionIsNestedWindowsProjection()
3233
Assert.Equal(typeDef.Attributes.IsNested(), typeDef.IsNested);
3334
}
3435
}
36+
37+
[Fact]
38+
public void ValidateDeclaringType()
39+
{
40+
var reader = MetadataReaderTests.GetMetadataReader(Misc.Members);
41+
42+
Assert.NotEmpty(reader.GetTypesWithEvents());
43+
Assert.NotEmpty(reader.GetTypesWithProperties());
44+
45+
foreach (var typeDefHandle in reader.TypeDefinitions)
46+
{
47+
var typeDef = reader.GetTypeDefinition(typeDefHandle);
48+
foreach (var nestedTypeHandle in typeDef.GetNestedTypes())
49+
{
50+
var nestedType = reader.GetTypeDefinition(nestedTypeHandle);
51+
Assert.Equal(typeDefHandle, nestedType.GetDeclaringType());
52+
}
53+
foreach (var fieldHandle in typeDef.GetFields())
54+
{
55+
var field = reader.GetFieldDefinition(fieldHandle);
56+
Assert.Equal(typeDefHandle, field.GetDeclaringType());
57+
}
58+
foreach (var methodHandle in typeDef.GetMethods())
59+
{
60+
var method = reader.GetMethodDefinition(methodHandle);
61+
Assert.Equal(typeDefHandle, method.GetDeclaringType());
62+
}
63+
foreach (var eventHandle in typeDef.GetEvents())
64+
{
65+
var eventDef = reader.GetEventDefinition(eventHandle);
66+
Assert.Equal(typeDefHandle, eventDef.GetDeclaringType());
67+
}
68+
foreach (var propertyHandle in typeDef.GetProperties())
69+
{
70+
var property = reader.GetPropertyDefinition(propertyHandle);
71+
Assert.Equal(typeDefHandle, property.GetDeclaringType());
72+
}
73+
}
74+
}
3575
}
3676
}

src/libraries/System.Reflection.Metadata/tests/PortableExecutable/PEReaderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ private unsafe void ValidateSectionData(PEReader reader)
245245
{
246246
0x00, 0x20, 0x00, 0x00,
247247
0x0C, 0x00, 0x00, 0x00,
248-
0xD0, 0x38, 0x00, 0x00
248+
0x00, 0x39, 0x00, 0x00
249249
}, relocBlob1);
250250

251251
AssertEx.Equal(relocBlob1, relocBlob2);

src/libraries/System.Reflection.Metadata/tests/Resources/Misc/Members.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public void MD1() { }
2121
public int PE1 { get { return 1; } set { } }
2222

2323
event System.Action ED1;
24+
25+
public class N
26+
{
27+
}
2428
}
2529

2630
public class E
Binary file not shown.

0 commit comments

Comments
 (0)