コード例 #1
0
        private static bool CanCastArrayTo(this ArrayType thisType, TypeDesc otherType, StackOverflowProtect protect)
        {
            // Casting the array to one of the base types or interfaces?
            if (otherType.IsDefType)
            {
                return(thisType.CanCastToClassOrInterface(otherType, protect));
            }

            // Casting array to something else (between SzArray and Array, for example)?
            if (thisType.Category != otherType.Category)
            {
                // An SzArray is castable to MdArray rank 1. We follow the same casting rules as SzArray to SzArray.
                if (thisType.Category == TypeFlags.SzArray &&
                    otherType.Category == TypeFlags.Array &&
                    ((ArrayType)otherType).Rank == 1)
                {
                    return(thisType.CanCastParamTo(((ArrayType)otherType).ParameterType, protect));
                }

                return(false);
            }

            ArrayType otherArrayType = (ArrayType)otherType;

            // Check ranks if we're casting multidim arrays
            if (!thisType.IsSzArray && thisType.Rank != otherArrayType.Rank)
            {
                return(false);
            }

            return(thisType.CanCastParamTo(otherArrayType.ParameterType, protect));
        }