Skip to content

Commit

Permalink
Cleanup reflection RuntimeType checks (dotnet#59457)
Browse files Browse the repository at this point in the history
* Delete IsRuntimeImplemented helper method

No longer necessary

* Use compact syntax for RuntimeType checks
  • Loading branch information
jkotas committed Sep 22, 2021
1 parent 22a1a61 commit 9dc499c
Show file tree
Hide file tree
Showing 26 changed files with 60 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ public static Assembly GetCallingAssembly()
return entryAssembly;
}

// Exists to faciliate code sharing between CoreCLR and CoreRT.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool IsRuntimeImplemented() => this is RuntimeAssembly;

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern uint GetAssemblyCount();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1131,9 +1131,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.GetCustomAttributes(m_bakedRuntimeType, attributeRuntimeType, inherit);
Expand All @@ -1147,9 +1145,7 @@ public override bool IsDefined(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.IsDefined(m_bakedRuntimeType, attributeRuntimeType, inherit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
Expand All @@ -312,9 +310,7 @@ public override bool IsDefined(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.IsDefined(this, attributeRuntimeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
Expand All @@ -188,9 +186,7 @@ public override bool IsDefined(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.IsDefined(this, attributeRuntimeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
Expand All @@ -94,9 +92,7 @@ public override bool IsDefined(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.IsDefined(this, attributeRuntimeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
Expand All @@ -79,9 +77,7 @@ public override bool IsDefined(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.IsDefined(this, attributeRuntimeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType, inherit);
Expand All @@ -261,9 +259,7 @@ public override bool IsDefined(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.IsDefined(this, attributeRuntimeType, inherit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
Expand All @@ -406,9 +404,7 @@ public override bool IsDefined(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.IsDefined(this, attributeRuntimeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
if (MdToken.IsNullToken(m_tkParamDef))
return Array.Empty<object>();

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
Expand All @@ -531,9 +529,7 @@ public override bool IsDefined(Type attributeType, bool inherit)
if (MdToken.IsNullToken(m_tkParamDef))
return false;

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.IsDefined(this, attributeRuntimeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.GetCustomAttributes(this, attributeRuntimeType);
Expand All @@ -155,9 +153,7 @@ public override bool IsDefined(Type attributeType, bool inherit)
if (attributeType == null)
throw new ArgumentNullException(nameof(attributeType));

RuntimeType? attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType;

if (attributeRuntimeType == null)
if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType));

return CustomAttribute.IsDefined(this, attributeRuntimeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ internal static unsafe bool ObjectHasComponentSize(object obj)
/// <returns>The allocated memory</returns>
public static IntPtr AllocateTypeAssociatedMemory(Type type, int size)
{
RuntimeType? rt = type as RuntimeType;
if (rt == null)
if (type is not RuntimeType rt)
throw new ArgumentException(SR.Arg_MustBeType, nameof(type));

if (size < 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3532,7 +3532,7 @@ public override Type MakeArrayType(int rank)
RuntimeType valueType;
Pointer? pointer = value as Pointer;
if (pointer != null)
valueType = (RuntimeType)pointer.GetPointerType();
valueType = pointer.GetPointerType();
else
valueType = (RuntimeType)value.GetType();

Expand Down Expand Up @@ -3573,7 +3573,7 @@ public override Type MakeArrayType(int rank)
RuntimeType valueType;
Pointer? pointer = value as Pointer;
if (pointer != null)
valueType = (RuntimeType)pointer.GetPointerType();
valueType = pointer.GetPointerType();
else
valueType = (RuntimeType)value.GetType();

Expand Down
4 changes: 0 additions & 4 deletions src/coreclr/System.Private.CoreLib/src/System/Type.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,5 @@ internal virtual RuntimeTypeHandle GetTypeHandleInternal()
[Intrinsic]
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern bool operator !=(Type? left, Type? right);

// Exists to faciliate code sharing between CoreCLR and CoreRT.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool IsRuntimeImplemented() => this is RuntimeType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public static partial class Activator
if (activationAttributes?.Length > 0)
throw new PlatformNotSupportedException(SR.NotSupported_ActivAttr);

if (type.UnderlyingSystemType is RuntimeType rt)
return rt.CreateInstanceImpl(bindingAttr, binder, args, culture);
if (type.UnderlyingSystemType is not RuntimeType rt)
throw new ArgumentException(SR.Arg_MustBeType, nameof(type));

throw new ArgumentException(SR.Arg_MustBeType, nameof(type));
return rt.CreateInstanceImpl(bindingAttr, binder, args, culture);
}

[System.Security.DynamicSecurityMethod]
Expand Down
14 changes: 7 additions & 7 deletions src/libraries/System.Private.CoreLib/src/System/DefaultBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ public sealed override FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo
for (i = 0; i < types.Length; i++)
{
realTypes[i] = types[i].UnderlyingSystemType;
if (!(realTypes[i].IsRuntimeImplemented() || realTypes[i] is SignatureType))
if (!(realTypes[i] is RuntimeType || realTypes[i] is SignatureType))
throw new ArgumentException(SR.Arg_MustBeType, nameof(types));
}
types = realTypes;
Expand Down Expand Up @@ -566,8 +566,8 @@ public sealed override FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo

if (pCls.IsPrimitive)
{
if (!type.UnderlyingSystemType.IsRuntimeImplemented() ||
!CanChangePrimitive(type.UnderlyingSystemType, pCls.UnderlyingSystemType))
if (type.UnderlyingSystemType is not RuntimeType rtType ||
!CanChangePrimitive(rtType, pCls.UnderlyingSystemType))
break;
}
else
Expand Down Expand Up @@ -653,8 +653,8 @@ public sealed override FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo

if (pCls.IsPrimitive)
{
if (!indexes[j].UnderlyingSystemType.IsRuntimeImplemented() ||
!CanChangePrimitive(indexes[j].UnderlyingSystemType, pCls.UnderlyingSystemType))
if (indexes[j].UnderlyingSystemType is not RuntimeType rtType ||
!CanChangePrimitive(rtType, pCls.UnderlyingSystemType))
break;
}
else
Expand All @@ -671,8 +671,8 @@ public sealed override FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo
{
if (candidates[i].PropertyType.IsPrimitive)
{
if (!returnType.UnderlyingSystemType.IsRuntimeImplemented() ||
!CanChangePrimitive(returnType.UnderlyingSystemType, candidates[i].PropertyType.UnderlyingSystemType))
if (returnType.UnderlyingSystemType is not RuntimeType rtType ||
!CanChangePrimitive(rtType, candidates[i].PropertyType.UnderlyingSystemType))
continue;
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,7 @@ private static RuntimeType ValidateRuntimeType(Type enumType)
throw new ArgumentNullException(nameof(enumType));
if (!enumType.IsEnum)
throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType));
if (!(enumType is RuntimeType rtType))
if (enumType is not RuntimeType rtType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType));
return rtType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ public sealed unsafe class Pointer : ISerializable
{
// CoreCLR: Do not add or remove fields without updating the ReflectionPointer class in runtimehandles.h
private readonly void* _ptr;
private readonly Type _ptrType;
private readonly RuntimeType _ptrType;

private Pointer(void* ptr, Type ptrType)
private Pointer(void* ptr, RuntimeType ptrType)
{
Debug.Assert(ptrType.IsRuntimeImplemented()); // CoreCLR: For CoreRT's sake, _ptrType has to be declared as "Type", but in fact, it is always a RuntimeType. Code on CoreCLR expects this.
_ptr = ptr;
_ptrType = ptrType;
}
Expand All @@ -27,10 +26,10 @@ public static object Box(void* ptr, Type type)
throw new ArgumentNullException(nameof(type));
if (!type.IsPointer)
throw new ArgumentException(SR.Arg_MustBePointer, nameof(ptr));
if (!type.IsRuntimeImplemented())
throw new ArgumentException(SR.Arg_MustBeType, nameof(ptr));
if (type is not RuntimeType rtType)
throw new ArgumentException(SR.Arg_MustBeType, nameof(type));

return new Pointer(ptr, type);
return new Pointer(ptr, rtType);
}

public static void* Unbox(object ptr)
Expand All @@ -57,7 +56,7 @@ void ISerializable.GetObjectData(SerializationInfo info, StreamingContext contex
throw new PlatformNotSupportedException();
}

internal Type GetPointerType() => _ptrType;
internal RuntimeType GetPointerType() => _ptrType;
internal IntPtr GetPointerValue() => (IntPtr)_ptr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public ResourceManager(string baseName, Assembly assembly)
throw new ArgumentNullException(nameof(baseName));
if (null == assembly)
throw new ArgumentNullException(nameof(assembly));
if (!assembly.IsRuntimeImplemented())
if (assembly is not RuntimeAssembly)
throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly);

MainAssembly = assembly;
Expand All @@ -211,7 +211,7 @@ public ResourceManager(string baseName, Assembly assembly,
throw new ArgumentNullException(nameof(baseName));
if (null == assembly)
throw new ArgumentNullException(nameof(assembly));
if (!assembly.IsRuntimeImplemented())
if (assembly is not RuntimeAssembly)
throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly);

MainAssembly = assembly;
Expand All @@ -228,7 +228,7 @@ public ResourceManager(Type resourceSource)
{
if (null == resourceSource)
throw new ArgumentNullException(nameof(resourceSource));
if (!resourceSource.IsRuntimeImplemented())
if (resourceSource is not RuntimeType)
throw new ArgumentException(SR.Argument_MustBeRuntimeType);

_locationInfo = resourceSource;
Expand Down
Loading

0 comments on commit 9dc499c

Please sign in to comment.