Beispiel #1
0
        static bool CanAssignArrayElement(Type source, Type target)
        {
            if (!target.IsValueType && !target.IsPointer)
            {
                if (!source.IsValueType && !source.IsPointer)
                {
                    // Reference to reference copy
                    return
                        (source.IsInterface || target.IsInterface ||
                         source.IsAssignableFrom(target) || target.IsAssignableFrom(source));
                }
                else
                {
                    // Value to reference copy
                    if (source.IsPointer)
                    {
                        return(false);
                    }
                    return(target.IsAssignableFrom(source));
                }
            }
            else
            {
                if (source.IsEquivalentTo(target))
                {
                    return(true);
                }
                else if (source.IsPointer && target.IsPointer)
                {
                    return(true);
                }
                else if (source.IsPrimitive && target.IsPrimitive)
                {
                    // Special case: normally C# doesn't allow implicit ushort->char cast).
                    if (source == typeof(ushort) && target == typeof(char))
                    {
                        return(true);
                    }

                    // Allow primitive type widening
                    return(DefaultBinder.CanChangePrimitive(source, target));
                }
                else if (!source.IsValueType && !source.IsPointer)
                {
                    // Source is base class or interface of destination type
                    if (target.IsPointer)
                    {
                        return(false);
                    }
                    return(source.IsAssignableFrom(target));
                }
            }

            return(false);
        }
Beispiel #2
0
        static bool CanAssignArrayElement(Type source, Type target)
        {
            if (!target.IsValueType && !target.IsPointer)
            {
                if (!source.IsValueType && !source.IsPointer)
                {
                    // Reference to reference copy
                    return
                        (source.IsInterface || target.IsInterface ||
                         source.IsAssignableFrom(target) || target.IsAssignableFrom(source));
                }
                else
                {
                    // Value to reference copy
                    if (source.IsPointer)
                    {
                        return(false);
                    }
                    return(target.IsAssignableFrom(source));
                }
            }
            else
            {
                if (source.IsEquivalentTo(target))
                {
                    return(true);
                }
                else if (source.IsPointer && target.IsPointer)
                {
                    return(true);
                }
                else if (source.IsPrimitive && target.IsPrimitive)
                {
                    // Allow primitive type widening
                    return(DefaultBinder.CanChangePrimitive(source, target));
                }
                else if (!source.IsValueType && !source.IsPointer)
                {
                    // Source is base class or interface of destination type
                    if (target.IsPointer)
                    {
                        return(false);
                    }
                    return(source.IsAssignableFrom(target));
                }
            }

            return(false);
        }