예제 #1
0
        public static TargetObject ImplicitConversion(MdbEvaluationContext ctx,
                                                      TargetObject obj, TargetType type)
        {
            if (obj.Type.Equals(type))
            {
                return(obj);
            }

            if (type is TargetObjectType || ObjectUtil.FixTypeName(type.Name) == "System.Object")
            {
                if (obj.Type.IsByRef)
                {
                    return(obj);
                }
                return(BoxValue(ctx, obj));
            }

            if (obj is TargetEnumObject && type is TargetFundamentalType)
            {
                TargetEnumObject e = (TargetEnumObject)obj;
                return(ImplicitConversion(ctx, e.GetValue(ctx.Thread), type));
            }

            if (type is TargetEnumType)
            {
                TargetEnumType e = (TargetEnumType)type;
                return(ImplicitConversion(ctx, obj, e.Value.Type));
            }

            if (obj is TargetArrayObject && type.Name == "System.Array")
            {
                return(obj);
            }

            if (obj is TargetArrayObject && type is TargetArrayType)
            {
                TargetArrayObject sa = (TargetArrayObject)obj;
                TargetArrayType   ta = (TargetArrayType)type;
                if (sa.Type.ElementType.Equals(ta.ElementType))
                {
                    return(obj);
                }
            }

            if ((obj is TargetFundamentalObject) && (type is TargetFundamentalType))
            {
                return(ImplicitFundamentalConversion(
                           ctx, (TargetFundamentalObject)obj,
                           (TargetFundamentalType)type));
            }

            if ((obj is TargetClassObject) && (type is TargetClassType))
            {
                return(ImplicitReferenceConversion(
                           ctx, (TargetClassObject)obj,
                           (TargetClassType)type));
            }

            return(null);
        }
예제 #2
0
        public static bool ImplicitConversionExists(MdbEvaluationContext ctx,
                                                    TargetType source, TargetType target)
        {
            if (source.Equals(target))
            {
                return(true);
            }

            if (source is TargetArrayType && target.Name == "System.Array")
            {
                return(true);
            }

            if (ObjectUtil.FixTypeName(target.Name) == "System.Object")
            {
                return(true);
            }

            if (source is TargetArrayType && target is TargetArrayType)
            {
                TargetArrayType sa = (TargetArrayType)source;
                TargetArrayType ta = (TargetArrayType)target;
                return(sa.ElementType.Equals(ta.ElementType));
            }

            if (source is TargetEnumType)
            {
                TargetEnumType e = (TargetEnumType)source;
                if (ImplicitConversionExists(ctx, e.Value.Type, target))
                {
                    return(true);
                }
            }

            if (target is TargetEnumType)
            {
                TargetEnumType e = (TargetEnumType)target;
                if (ImplicitConversionExists(ctx, source, e.Value.Type))
                {
                    return(true);
                }
            }

            if ((source is TargetFundamentalType) && (target is TargetFundamentalType))
            {
                return(ImplicitFundamentalConversionExists(
                           (TargetFundamentalType)source,
                           (TargetFundamentalType)target));
            }

            if ((source is TargetClassType) && (target is TargetClassType))
            {
                return(ImplicitReferenceConversionExists(
                           ctx, (TargetClassType)source,
                           (TargetClassType)target));
            }

            return(false);
        }
예제 #3
0
        public static TargetObject Cast(MdbEvaluationContext ctx, TargetObject obj, TargetType targetType)
        {
            obj = ObjectUtil.GetRealObject(ctx, obj);

            if (obj.Type == targetType)
            {
                return(obj);
            }

            if (targetType is TargetObjectType || ObjectUtil.FixTypeName(targetType.Name) == "System.Object")
            {
                if (obj.Type.IsByRef)
                {
                    return(obj);
                }
                return(BoxValue(ctx, obj));
            }

            if (targetType is TargetPointerType)
            {
                throw new NotSupportedException();
            }

            if (targetType is TargetFundamentalType)
            {
                TargetFundamentalObject fobj = obj as TargetFundamentalObject;
                if (fobj == null)
                {
                    throw new NotSupportedException();
                }

                TargetFundamentalType ftype = targetType as TargetFundamentalType;
                TargetObject          ob    = ExplicitFundamentalConversion(ctx, fobj, ftype);
                if (ob == null)
                {
                    throw new NotSupportedException();
                }
                return(ob);
            }

            if (targetType is TargetNullableType)
            {
                TargetNullableType ntype = (TargetNullableType)targetType;
                if (obj.Kind == TargetObjectKind.Null)
                {
                    return(obj);
                }
                else if (obj.Kind != TargetObjectKind.Nullable)
                {
                    return(ImplicitConversion(ctx, obj, ntype.ElementType));
                }

                TargetNullableType ntype2 = (TargetNullableType)obj.Type;
                if (ImplicitConversionExists(ctx, ntype2.ElementType, ntype.ElementType))
                {
                    return(obj);
                }
            }

            TargetClassType   ctype  = ToClassType(targetType);
            TargetClassObject source = ToClassObject(ctx, obj);

            if (source == null)
            {
                throw new Exception(string.Format("Variable is not a class type."));
            }

            return(TryCast(ctx, source, ctype));
        }