mirror of
https://github.com/BepInEx/mono.git
synced 2026-11-16 01:41:18 -10:00
[reflection] Implement RuntimeType.IsSubclassOf in native (#10012)
* [reflection] Implement RuntimeType.IsSubclassOf in native Fixes a performance regression https://github.com/mono/mono/issues/10000 * fixup - byref and reflexive IsSubclassOf
This commit is contained in:
1 parent
32d9b9ec3a
commit
e26c958f3b
8 files changed
+148
No files matched your search
@@ -812,5 +812,20 @@ namespace System
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
[System.Runtime.InteropServices.ComVisible(true)]
|
||||
[Pure]
|
||||
public override bool IsSubclassOf(Type type)
|
||||
{
|
||||
if ((object)type == null)
|
||||
throw new ArgumentNullException("type");
|
||||
Contract.EndContractBlock();
|
||||
RuntimeType rtType = type as RuntimeType;
|
||||
if (rtType == null)
|
||||
return false;
|
||||
|
||||
return RuntimeTypeHandle.IsSubclassOf (this, rtType);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -250,5 +250,13 @@ namespace System
|
||||
[MethodImplAttribute(MethodImplOptions.InternalCall)]
|
||||
internal extern static IntPtr GetGenericParameterInfo (RuntimeType type);
|
||||
|
||||
internal static bool IsSubclassOf (RuntimeType childType, RuntimeType baseType)
|
||||
{
|
||||
return is_subclass_of (childType._impl.Value, baseType._impl.Value);
|
||||
}
|
||||
|
||||
[MethodImplAttribute(MethodImplOptions.InternalCall)]
|
||||
internal extern static bool is_subclass_of (IntPtr childType, IntPtr baseType);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -477,6 +477,11 @@ namespace MonoTests.System
|
||||
Assert.IsNull (paramType.BaseType, "#02-b");
|
||||
Assert.IsTrue (paramType.IsSubclassOf (typeof (Object)), "#03");
|
||||
Assert.IsFalse (paramType.IsSubclassOf (paramType), "#04");
|
||||
|
||||
// IsSubclassOf is not reflexive
|
||||
Assert.IsFalse (typeof (string).IsSubclassOf (typeof (string)), "#05");
|
||||
Assert.IsFalse (typeof (int).IsSubclassOf (typeof (int)), "#06");
|
||||
Assert.IsFalse (typeof (object).IsSubclassOf (typeof (object)), "#07");
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -3703,6 +3703,7 @@ namespace System
|
||||
return RuntimeTypeHandle.IsInstanceOfType(this, o);
|
||||
}
|
||||
|
||||
#if !MONO
|
||||
[System.Runtime.InteropServices.ComVisible(true)]
|
||||
[Pure]
|
||||
public override bool IsSubclassOf(Type type)
|
||||
@@ -3732,6 +3733,7 @@ namespace System
|
||||
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
public override bool IsAssignableFrom(System.Reflection.TypeInfo typeInfo){
|
||||
if(typeInfo==null) return false;
|
||||
|
||||
@@ -875,6 +875,9 @@ mono_metadata_free_inflated_signature (MonoMethodSignature *sig);
|
||||
MonoMethodSignature*
|
||||
mono_inflate_generic_signature (MonoMethodSignature *sig, MonoGenericContext *context, MonoError *error);
|
||||
|
||||
MonoClass*
|
||||
mono_generic_param_get_base_type (MonoClass *klass);
|
||||
|
||||
typedef struct {
|
||||
MonoImage *corlib;
|
||||
MonoClass *object_class;
|
||||
@@ -1002,6 +1005,8 @@ GENERATE_GET_CLASS_WITH_CACHE_DECL (variant)
|
||||
|
||||
GENERATE_GET_CLASS_WITH_CACHE_DECL (appdomain_unloaded_exception)
|
||||
|
||||
GENERATE_GET_CLASS_WITH_CACHE_DECL (valuetype)
|
||||
|
||||
/* If you need a MonoType, use one of the mono_get_*_type () functions in class-inlines.h */
|
||||
extern MonoDefaults mono_defaults;
|
||||
|
||||
|
||||
@@ -71,6 +71,7 @@ static MonoClass *mono_class_from_mono_type_internal (MonoType *type);
|
||||
|
||||
static gboolean mono_class_is_subclass_of_internal (MonoClass *klass, MonoClass *klassc, gboolean check_interfaces);
|
||||
|
||||
GENERATE_GET_CLASS_WITH_CACHE (valuetype, "System", "ValueType")
|
||||
|
||||
static
|
||||
MonoImage *
|
||||
@@ -3824,6 +3825,58 @@ mono_class_is_assignable_from_slow (MonoClass *target, MonoClass *candidate)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* mono_generic_param_get_base_type:
|
||||
*
|
||||
* Return the base type of the given generic parameter from its constraints.
|
||||
*
|
||||
* Could be another generic parameter, or it could be Object or ValueType.
|
||||
*/
|
||||
MonoClass*
|
||||
mono_generic_param_get_base_type (MonoClass *klass)
|
||||
{
|
||||
MonoType *type = m_class_get_byval_arg (klass);
|
||||
g_assert (mono_type_is_generic_argument (type));
|
||||
|
||||
MonoGenericParam *gparam = type->data.generic_param;
|
||||
|
||||
MonoClass **constraints = mono_generic_container_get_param_info (gparam->owner, gparam->num)->constraints;
|
||||
|
||||
MonoClass *base_class = mono_defaults.object_class;
|
||||
|
||||
if (constraints) {
|
||||
int i;
|
||||
for (i = 0; constraints [i]; ++i) {
|
||||
MonoClass *constraint = constraints[i];
|
||||
|
||||
if (MONO_CLASS_IS_INTERFACE (constraint))
|
||||
continue;
|
||||
|
||||
MonoType *constraint_type = m_class_get_byval_arg (constraint);
|
||||
if (mono_type_is_generic_argument (constraint_type)) {
|
||||
MonoGenericParam *constraint_param = constraint_type->data.generic_param;
|
||||
MonoGenericParamInfo *constraint_info = mono_generic_param_info (constraint_param);
|
||||
if ((constraint_info->flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) == 0 &&
|
||||
(constraint_info->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) == 0)
|
||||
continue;
|
||||
}
|
||||
|
||||
base_class = constraint;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (base_class == mono_defaults.object_class)
|
||||
{
|
||||
MonoGenericParamInfo *gparam_info = mono_generic_param_info (gparam);
|
||||
if ((gparam_info->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) != 0) {
|
||||
base_class = mono_class_get_valuetype_class ();
|
||||
}
|
||||
}
|
||||
|
||||
return base_class;
|
||||
}
|
||||
|
||||
/**
|
||||
* mono_class_get_cctor:
|
||||
* \param klass A MonoClass pointer
|
||||
|
||||
@@ -831,6 +831,7 @@ HANDLES(ICALL(RTH_14, "IsGenericVariable", ves_icall_RuntimeTypeHandle_IsGeneric
|
||||
HANDLES(ICALL(RTH_15, "IsInstanceOfType", ves_icall_RuntimeTypeHandle_IsInstanceOfType))
|
||||
HANDLES(ICALL(RTH_16, "IsPointer", ves_icall_RuntimeTypeHandle_IsPointer))
|
||||
HANDLES(ICALL(RTH_17, "IsPrimitive", ves_icall_RuntimeTypeHandle_IsPrimitive))
|
||||
NOHANDLES(ICALL(RTH_17a, "is_subclass_of", ves_icall_RuntimeTypeHandle_is_subclass_of))
|
||||
HANDLES(ICALL(RTH_18, "type_is_assignable_from", ves_icall_RuntimeTypeHandle_type_is_assignable_from))
|
||||
|
||||
ICALL_TYPE(RNG, "System.Security.Cryptography.RNGCryptoServiceProvider", RNG_1)
|
||||
|
||||
@@ -1752,6 +1752,65 @@ ves_icall_RuntimeTypeHandle_type_is_assignable_from (MonoReflectionTypeHandle re
|
||||
return mono_class_is_assignable_from (klass, klassc);
|
||||
}
|
||||
|
||||
ICALL_EXPORT MonoBoolean
|
||||
ves_icall_RuntimeTypeHandle_is_subclass_of (MonoType *childType, MonoType *baseType)
|
||||
{
|
||||
ERROR_DECL (error);
|
||||
mono_bool result = FALSE;
|
||||
MonoClass *childClass;
|
||||
MonoClass *baseClass;
|
||||
|
||||
childClass = mono_class_from_mono_type (childType);
|
||||
mono_class_init_checked (childClass, error);
|
||||
goto_if_nok (error, done);
|
||||
|
||||
baseClass = mono_class_from_mono_type (baseType);
|
||||
mono_class_init_checked (baseClass, error);
|
||||
goto_if_nok (error, done);
|
||||
|
||||
if (G_UNLIKELY (childType->byref)) {
|
||||
result = !baseType->byref && baseClass == mono_defaults.object_class;
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (G_UNLIKELY (baseType->byref)) {
|
||||
result = FALSE;
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (childType == baseType) {
|
||||
/* .NET IsSubclassOf is not reflexive */
|
||||
result = FALSE;
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (G_UNLIKELY (is_generic_parameter (childType))) {
|
||||
/* slow path: walk the type hierarchy looking at base types
|
||||
* until we see baseType. If the current type is not a gparam,
|
||||
* break out of the loop and use is_subclass_of.
|
||||
*/
|
||||
MonoClass *c = mono_generic_param_get_base_type (childClass);
|
||||
|
||||
result = FALSE;
|
||||
while (c != NULL) {
|
||||
if (c == baseClass) {
|
||||
result = TRUE;
|
||||
break;
|
||||
}
|
||||
if (!is_generic_parameter (m_class_get_byval_arg (c))) {
|
||||
result = mono_class_is_subclass_of (c, baseClass, FALSE);
|
||||
break;
|
||||
} else
|
||||
c = mono_generic_param_get_base_type (c);
|
||||
}
|
||||
} else {
|
||||
result = mono_class_is_subclass_of (childClass, baseClass, FALSE);
|
||||
}
|
||||
done:
|
||||
mono_error_set_pending_exception (error);
|
||||
return result;
|
||||
}
|
||||
|
||||
ICALL_EXPORT guint32
|
||||
ves_icall_RuntimeTypeHandle_IsInstanceOfType (MonoReflectionTypeHandle ref_type, MonoObjectHandle obj, MonoError *error)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user