Пример #1
0
        private static bool CanCastParamTo(this ParameterizedType thisType, TypeDesc paramType, StackOverflowProtect protect)
        {
            // While boxed value classes inherit from object their
            // unboxed versions do not.  Parameterized types have the
            // unboxed version, thus, if the from type parameter is value
            // class then only an exact match/equivalence works.
            if (thisType.ParameterType == paramType)
            {
                return(true);
            }

            TypeDesc curTypesParm = thisType.ParameterType;

            // Object parameters don't need an exact match but only inheritance, check for that
            TypeDesc fromParamUnderlyingType = curTypesParm.UnderlyingType;

            if (fromParamUnderlyingType.IsGCPointer)
            {
                return(curTypesParm.CanCastToInternal(paramType, protect));
            }
            else if (curTypesParm.IsGenericParameter)
            {
                var genericVariableFromParam = (GenericParameterDesc)curTypesParm;
                if (genericVariableFromParam.HasReferenceTypeConstraint)
                {
                    return(genericVariableFromParam.CanCastToInternal(paramType, protect));
                }
            }
            else if (fromParamUnderlyingType.IsPrimitive)
            {
                TypeDesc toParamUnderlyingType = paramType.UnderlyingType;
                if (toParamUnderlyingType.IsPrimitive)
                {
                    if (toParamUnderlyingType == fromParamUnderlyingType)
                    {
                        return(true);
                    }

                    if (ArePrimitveTypesEquivalentSize(fromParamUnderlyingType, toParamUnderlyingType))
                    {
                        return(true);
                    }
                }
            }

            // Anything else is not a match
            return(false);
        }
Пример #2
0
        private static bool IsBoxedAndCanCastTo(this TypeDesc thisType, TypeDesc otherType, StackOverflowProtect protect)
        {
            TypeDesc fromUnderlyingType = thisType.UnderlyingType;

            if (fromUnderlyingType.IsGCPointer)
            {
                return(thisType.CanCastToInternal(otherType, protect));
            }
            else if (thisType.IsGenericParameter)
            {
                var genericVariableFromParam = (GenericParameterDesc)thisType;
                if (genericVariableFromParam.HasReferenceTypeConstraint)
                {
                    return(genericVariableFromParam.CanCastToInternal(otherType, protect));
                }
            }

            return(false);
        }
Пример #3
0
 /// <summary>
 /// Returns true if '<paramref name="thisType"/>' can be cast to '<paramref name="otherType"/>'.
 /// Assumes '<paramref name="thisType"/>' is in it's boxed form if it's a value type (i.e.
 /// [System.Int32].CanCastTo([System.Object]) will return true).
 /// </summary>
 public static bool CanCastTo(this TypeDesc thisType, TypeDesc otherType)
 {
     return(thisType.CanCastToInternal(otherType, null));
 }