diff --git a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets
index 74c2b457b9ad8..ae077a3960007 100644
--- a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets
+++ b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets
@@ -108,7 +108,6 @@ The .NET Foundation licenses this file to you under the MIT license.
-
diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/MetadataOnlyType.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/MetadataOnlyType.cs
index cc75ded1b1185..f8a56721f8e3d 100644
--- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/MetadataOnlyType.cs
+++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/MetadataOnlyType.cs
@@ -72,6 +72,9 @@ public override Array GetEnumValuesAsUnderlyingType()
throw new ArgumentException(SR.Arg_MustBeEnum, "enumType");
}
+ public override int GetHashCode()
+ => RuntimeHelpers.GetHashCode(this);
+
public override RuntimeTypeHandle TypeHandle
=> GetRuntimeTypeInfo().TypeHandle;
@@ -183,6 +186,11 @@ public override Type GetFunctionPointerReturnType()
// Implementation shared with MetadataType
//
+ public override string ToString()
+ => GetRuntimeTypeInfo().ToString();
+
+ public override bool Equals(object? obj) => ReferenceEquals(obj, this);
+
object ICloneable.Clone() => this;
public override bool IsSecurityCritical => true;
@@ -289,6 +297,8 @@ public override IEnumerable CustomAttributes
public override IList GetCustomAttributesData()
=> GetRuntimeTypeInfo().GetCustomAttributesData();
+ public override string Name => GetRuntimeTypeInfo().Name;
+
public override string? Namespace => GetRuntimeTypeInfo().Namespace;
public override string? AssemblyQualifiedName => GetRuntimeTypeInfo().AssemblyQualifiedName;
@@ -301,8 +311,6 @@ public override IList GetCustomAttributesData()
public override Guid GUID => GetRuntimeTypeInfo().GUID;
- public override string Name => GetRuntimeTypeInfo().Name;
-
public override bool HasSameMetadataDefinitionAs(MemberInfo other) => GetRuntimeTypeInfo().HasSameMetadataDefinitionAs(other);
public override Type MakePointerType()
diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeType.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeType.cs
index 823768ad9939f..35c8e3bbe447f 100644
--- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeType.cs
+++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeType.cs
@@ -31,8 +31,17 @@ internal RuntimeType(MethodTable* pUnderlyingEEType)
internal RuntimeTypeInfo GetRuntimeTypeInfo() => _runtimeTypeInfo ?? CreateRuntimeTypeInfo();
+ private static bool IsReflectionDisabled => false;
+
+ private static bool DoNotThrowForNames => AppContext.TryGetSwitch("Switch.System.Reflection.Disabled.DoNotThrowForNames", out bool doNotThrow) && doNotThrow;
+ private static bool DoNotThrowForAssembly => AppContext.TryGetSwitch("Switch.System.Reflection.Disabled.DoNotThrowForAssembly", out bool doNotThrow) && doNotThrow;
+ private static bool DoNotThrowForAttributes => AppContext.TryGetSwitch("Switch.System.Reflection.Disabled.DoNotThrowForAttributes", out bool doNotThrow) && doNotThrow;
+
private RuntimeTypeInfo CreateRuntimeTypeInfo()
{
+ if (IsReflectionDisabled)
+ throw new NotSupportedException(SR.Reflection_Disabled);
+
EETypePtr eeType = ToEETypePtr();
RuntimeTypeHandle runtimeTypeHandle = new RuntimeTypeHandle(eeType);
@@ -186,6 +195,9 @@ public override Array GetEnumValuesAsUnderlyingType()
return Enum.GetValuesAsUnderlyingType(this);
}
+ public override int GetHashCode()
+ => ((nuint)_pUnderlyingEEType).GetHashCode();
+
public override RuntimeTypeHandle TypeHandle
=> new RuntimeTypeHandle(_pUnderlyingEEType);
@@ -267,7 +279,7 @@ public override Type[] GetInterfaces()
}
public override bool IsTypeDefinition
- => (_pUnderlyingEEType->IsCanonical && !_pUnderlyingEEType->IsGeneric) || _pUnderlyingEEType->IsGenericTypeDefinition;
+ => (_pUnderlyingEEType->IsCanonical && !_pUnderlyingEEType->IsGeneric) || _pUnderlyingEEType->IsGenericTypeDefinition;
public override bool IsGenericType
=> _pUnderlyingEEType->IsGeneric || _pUnderlyingEEType->IsGenericTypeDefinition;
@@ -375,6 +387,16 @@ public override Type GetFunctionPointerReturnType()
// Implementation shared with MetadataType
//
+ public override string ToString()
+ {
+ if (IsReflectionDisabled)
+ return "0x" + ((nuint)_pUnderlyingEEType).ToString("x");
+
+ return GetRuntimeTypeInfo().ToString();
+ }
+
+ public override bool Equals(object? obj) => ReferenceEquals(obj, this);
+
object ICloneable.Clone() => this;
public override bool IsSecurityCritical => true;
@@ -470,31 +492,81 @@ public override bool IsDefined(Type attributeType, bool inherit)
=> GetRuntimeTypeInfo().IsDefined(attributeType, inherit);
public override object[] GetCustomAttributes(bool inherit)
- => GetRuntimeTypeInfo().GetCustomAttributes(inherit);
+ {
+ if (IsReflectionDisabled && DoNotThrowForAttributes)
+ return Array.Empty