예제 #1
0
        static bool __CanCastTo(DmdType from, DmdType to)
        {
            if ((object)from == null)
            {
                throw new ArgumentNullException(nameof(from));
            }
            if ((object)to == null)
            {
                throw new ArgumentNullException(nameof(to));
            }

            bool res;
            var  r = from.__CanCastToNoGC(to);

            if (r == __CastResult.MaybeCast)
            {
                res = from.__CanCastTo(to);
            }
            else
            {
                res = r == __CastResult.CanCast;
            }

            if (!res && to.IsNullable && !from.__IsTypeDesc())
            {
                if (__IsNullableForType(to, from))
                {
                    res = true;
                }
            }

            return(res);
        }
예제 #2
0
 static bool __IsNullableForType(DmdType type, DmdType param)
 {
     if (type.__IsTypeDesc())
     {
         return(false);
     }
     if (!type.IsConstructedGenericType)
     {
         return(false);
     }
     return(__IsNullableForTypeHelper(type, param));
 }
예제 #3
0
        bool __CanCastTo(DmdType type)
        {
            if (this == type)
            {
                return(true);
            }

            if (__IsTypeDesc())
            {
                return(__TypeDesc_CanCastTo(type));
            }

            if (type.__IsTypeDesc())
            {
                return(false);
            }

            return(__CanCastToClassOrInterface(type));
        }
예제 #4
0
        __CastResult __CanCastToNoGC(DmdType type)
        {
            if (this == type)
            {
                return(__CastResult.CanCast);
            }

            if (__IsTypeDesc())
            {
                return(__TypeDesc_CanCastToNoGC(type));
            }

            if (type.__IsTypeDesc())
            {
                return(__CastResult.CannotCast);
            }

            return(__CanCastToClassOrInterfaceNoGC(type));
        }
예제 #5
0
        __CastResult __TypeDesc_CanCastToNoGC(DmdType toType)
        {
            Debug.Assert(this != toType);

            if (IsGenericParameter)
            {
                if (toType == AppDomain.System_Object)
                {
                    return(__CastResult.CanCast);
                }

                if (toType == AppDomain.System_ValueType)
                {
                    return(__CastResult.MaybeCast);
                }

                foreach (var constraint in GetGenericParameterConstraints())
                {
                    if (constraint.__CanCastToNoGC(toType) == __CastResult.CanCast)
                    {
                        return(__CastResult.CanCast);
                    }
                }
                return(__CastResult.MaybeCast);
            }

            if (!toType.__IsTypeDesc())
            {
                if (!IsArray)
                {
                    return(__CastResult.CannotCast);
                }
                return(__CanCastToClassOrInterfaceNoGC(toType));
            }

            var toKind   = toType.__GetInternalCorElementType();
            var fromKind = __GetInternalCorElementType();

            if (!(toKind == fromKind || (toKind == DDN.ElementType.Array && fromKind == DDN.ElementType.SZArray)))
            {
                return(__CastResult.CannotCast);
            }

            switch (toKind)
            {
            case DDN.ElementType.Array:
                if (GetArrayRank() != toType.GetArrayRank())
                {
                    return(__CastResult.CannotCast);
                }
                goto case DDN.ElementType.SZArray;

            case DDN.ElementType.SZArray:
            case DDN.ElementType.ByRef:
            case DDN.ElementType.Ptr:
                return(__CanCastParamNoGC(GetElementType(), toType.GetElementType()));

            case DDN.ElementType.Var:
            case DDN.ElementType.MVar:
            case DDN.ElementType.FnPtr:
                return(__CastResult.CannotCast);

            default:
                return(__CastResult.CanCast);
            }
        }
예제 #6
0
        bool __TypeDesc_CanCastTo(DmdType toType)
        {
            if (this == toType)
            {
                return(true);
            }

            if (IsGenericParameter)
            {
                if (toType == AppDomain.System_Object)
                {
                    return(true);
                }

                if (toType == AppDomain.System_ValueType)
                {
                    if ((GenericParameterAttributes & DmdGenericParameterAttributes.NotNullableValueTypeConstraint) != 0)
                    {
                        return(true);
                    }
                }

                foreach (var constraint in GetGenericParameterConstraints())
                {
                    if (constraint.__CanCastTo(toType))
                    {
                        return(true);
                    }
                }
                return(false);
            }

            if (!toType.__IsTypeDesc())
            {
                if (!IsArray)
                {
                    return(false);
                }

                if (__CanCastToClassOrInterface(toType))
                {
                    return(true);
                }

                if (toType.IsInterface)
                {
                    if (__ArraySupportsBizarreInterface(this, toType))
                    {
                        return(true);
                    }
                }

                return(false);
            }

            var toKind   = toType.__GetInternalCorElementType();
            var fromKind = __GetInternalCorElementType();

            if (!(toKind == fromKind || (toKind == DDN.ElementType.Array && fromKind == DDN.ElementType.SZArray)))
            {
                return(false);
            }

            switch (toKind)
            {
            case DDN.ElementType.Array:
                if (GetArrayRank() != toType.GetArrayRank())
                {
                    return(false);
                }
                goto case DDN.ElementType.SZArray;

            case DDN.ElementType.SZArray:
            case DDN.ElementType.ByRef:
            case DDN.ElementType.Ptr:
                return(__CanCastParam(GetElementType(), toType.GetElementType()));

            case DDN.ElementType.Var:
            case DDN.ElementType.MVar:
            case DDN.ElementType.FnPtr:
                return(false);

            default:
                return(true);
            }
        }