Skip to content

Commit 6944091

Browse files
Venkad000jkotas
andauthored
Add missing overrides for IsCollectible (#115785)
* Add missing overrides for IsCollectible * Add basic IsCollectible test cases for all reflection types Co-authored-by: Jan Kotas <[email protected]>
1 parent c00b15f commit 6944091

File tree

9 files changed

+64
-0
lines changed

9 files changed

+64
-0
lines changed

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeConstructorInfo.CoreCLR.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public override IList<CustomAttributeData> GetCustomAttributesData()
173173
internal RuntimeType GetRuntimeType() { return m_declaringType; }
174174
internal RuntimeModule GetRuntimeModule() { return RuntimeTypeHandle.GetModule(m_declaringType); }
175175
internal RuntimeAssembly GetRuntimeAssembly() { return GetRuntimeModule().GetRuntimeAssembly(); }
176+
public override bool IsCollectible => m_declaringType.IsCollectible;
176177
#endregion
177178

178179
#region MethodBase Overrides

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public override IList<CustomAttributeData> GetCustomAttributesData()
129129
public override int MetadataToken => m_token;
130130
public override Module Module => GetRuntimeModule();
131131
internal RuntimeModule GetRuntimeModule() { return m_declaringType.GetRuntimeModule(); }
132+
public override bool IsCollectible => m_declaringType.IsCollectible;
132133
#endregion
133134

134135
#region EventInfo Overrides

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/ThunkedApis.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ internal abstract partial class RuntimeConstructorInfo
9595
public sealed override bool IsSecurityCritical => true;
9696
public sealed override bool IsSecuritySafeCritical => false;
9797
public sealed override bool IsSecurityTransparent => false;
98+
99+
public sealed override bool IsCollectible => false;
98100
}
99101
}
100102

@@ -105,6 +107,8 @@ internal abstract partial class RuntimeEventInfo
105107
public sealed override MethodInfo GetAddMethod(bool nonPublic) => AddMethod.FilterAccessor(nonPublic);
106108
public sealed override MethodInfo GetRemoveMethod(bool nonPublic) => RemoveMethod.FilterAccessor(nonPublic);
107109
public sealed override MethodInfo GetRaiseMethod(bool nonPublic) => RaiseMethod?.FilterAccessor(nonPublic);
110+
111+
public sealed override bool IsCollectible => false;
108112
}
109113
}
110114

@@ -119,6 +123,16 @@ internal abstract partial class RuntimeMethodInfo
119123
public sealed override bool IsSecurityCritical => true;
120124
public sealed override bool IsSecuritySafeCritical => false;
121125
public sealed override bool IsSecurityTransparent => false;
126+
127+
public sealed override bool IsCollectible => false;
128+
}
129+
}
130+
131+
namespace System.Reflection.Runtime.FieldInfos
132+
{
133+
internal abstract partial class RuntimeFieldInfo
134+
{
135+
public sealed override bool IsCollectible => false;
122136
}
123137
}
124138

@@ -145,6 +159,8 @@ public sealed override MethodInfo[] GetAccessors(bool nonPublic)
145159
accessors[index++] = setter;
146160
return accessors;
147161
}
162+
163+
public sealed override bool IsCollectible => false;
148164
}
149165
}
150166

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/IsCollectibleTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,40 @@ public TestAssemblyLoadContext() : base(true) {}
1717
protected override Assembly Load(AssemblyName assemblyName) => null;
1818
}
1919

20+
public class BasicIsCollectibleTests
21+
{
22+
[Fact]
23+
public void CoreLib_IsCollectibleFalse()
24+
{
25+
Assert.False(typeof(object).IsCollectible);
26+
Assert.False(typeof(Console).Assembly.IsCollectible);
27+
Assert.False(typeof(Math).Assembly.IsCollectible);
28+
Assert.False(typeof(string).Assembly.IsCollectible);
29+
Assert.False(typeof(List<>).Assembly.IsCollectible);
30+
Assert.False(typeof(object).Assembly.IsCollectible);
31+
Assert.False(typeof(IDisposable).Assembly.IsCollectible);
32+
Assert.False(typeof(Span<>).Assembly.IsCollectible);
33+
Assert.False(typeof(Enumerable).Assembly.IsCollectible);
34+
Assert.False(typeof(string).GetMethods().First(m => m.Name == "Substring" && m.GetParameters().Length == 1).IsCollectible);
35+
Assert.False(typeof(Dictionary<,>).Assembly.IsCollectible);
36+
Assert.False(typeof(object).GetMethod("ToString").IsCollectible);
37+
Assert.False(typeof(string).GetMethod("Contains", new[] { typeof(string) }).IsCollectible);
38+
Assert.False(typeof(IntPtr).GetField("Zero").IsCollectible);
39+
Assert.False(typeof(IntPtr).GetProperty("MaxValue").IsCollectible);
40+
Assert.False(typeof(IntPtr).GetProperty("MinValue").IsCollectible);
41+
Assert.False(typeof(DateTime).GetProperty("Now").IsCollectible);
42+
Assert.False(typeof(AppDomain).GetMethod("GetData").IsCollectible);
43+
Assert.False(typeof(AppDomain).GetMethod("ToString").IsCollectible);
44+
Assert.False(typeof(string).GetConstructor(new[] { typeof(char[]), typeof(int), typeof(int) })!.IsCollectible);
45+
Assert.False(typeof(string).GetConstructor(new[] { typeof(char), typeof(int) })!.IsCollectible);
46+
Assert.False(typeof(TimeSpan).GetConstructor(new[] { typeof(int), typeof(int), typeof(int) })!.IsCollectible);
47+
Assert.False(typeof(Uri).GetConstructor(new[] { typeof(string) })!.IsCollectible);
48+
Assert.False(typeof(AppDomain).GetEvent("AssemblyLoad")!.IsCollectible);
49+
Assert.False(typeof(AppDomain).GetEvent("ProcessExit")!.IsCollectible);
50+
51+
}
52+
}
53+
2054
[ActiveIssue("https://github.com/mono/mono/issues/15142", TestRuntimes.Mono)]
2155
public class IsCollectibleTests
2256
{

src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ internal RuntimeModule GetRuntimeModule()
8787
return GetDeclaringTypeInternal().GetRuntimeModule();
8888
}
8989

90+
public override bool IsCollectible => false;
91+
9092
internal BindingFlags GetBindingFlags()
9193
{
9294
MonoEventInfo info = GetEventInfo(this);

src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeFieldInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ internal RuntimeModule GetRuntimeModule()
6969
return GetDeclaringTypeInternal().GetRuntimeModule();
7070
}
7171

72+
public override bool IsCollectible => false;
73+
7274
[MethodImplAttribute(MethodImplOptions.InternalCall)]
7375
internal extern override object UnsafeGetValue(object obj);
7476

src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.Mono.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ public override Module Module
181181
}
182182
}
183183

184+
public override bool IsCollectible => false;
185+
184186
private string FormatNameAndSig()
185187
{
186188
// Serialization uses ToString to resolve MethodInfo overloads.
@@ -747,6 +749,8 @@ public override Module Module
747749
}
748750
}
749751

752+
public override bool IsCollectible => false;
753+
750754
internal RuntimeModule GetRuntimeModule()
751755
{
752756
return RuntimeTypeHandle.GetModule((RuntimeType)DeclaringType);

src/mono/System.Private.CoreLib/src/System/Reflection/RuntimePropertyInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ internal RuntimeModule GetRuntimeModule()
136136
return GetDeclaringTypeInternal().GetRuntimeModule();
137137
}
138138

139+
public override bool IsCollectible => false;
140+
139141
#region Object Overrides
140142
public override string ToString()
141143
{

src/mono/System.Private.CoreLib/src/System/RuntimeType.Mono.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,8 @@ protected override bool IsValueTypeImpl()
13271327
// Returns true for generic parameters with the Enum constraint.
13281328
public override bool IsEnum => GetBaseType() == EnumType;
13291329

1330+
public override bool IsCollectible => false;
1331+
13301332
// Returns true for actual enum types only, ignoring generic parameter constraints.
13311333
internal bool IsActualEnum
13321334
{

0 commit comments

Comments
 (0)