Beispiel #1
0
            internal override Expression VisitMemberAccess(MemberExpression m)
            {
                Debug.Assert(m != null, "m != null");

                if (ClientConvert.IsKnownNullableType(m.Expression.Type))
                {
                    return(base.VisitMemberAccess(m));
                }

                if (!CommonUtil.IsClientType(m.Expression.Type))
                {
                    throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, SR.ALinqExpressionNotSupportedInProjection, this.type, m.ToString()));
                }

                PropertyInfo pi = null;

                if (ResourceBinder.PatternRules.MatchNonPrivateReadableProperty(m, out pi))
                {
                    Expression e = base.VisitMemberAccess(m);
                    this.box.AppendToPath(pi);
                    return(e);
                }

                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, SR.ALinqExpressionNotSupportedInProjection, this.type, m.ToString()));
            }
Beispiel #2
0
        private static int IndexOfStorage(Type type)
        {
            int index = ClientConvert.IndexOfReference(ClientConvert.KnownTypes, type);

#if !ASTORIA_LIGHT
            if ((index < 0) && needSystemDataLinqBinary && (type.Name == "Binary"))
            {
                return(LoadSystemDataLinqBinary(type));
            }
#endif
            return(index);
        }
Beispiel #3
0
        internal override Expression VisitConstant(ConstantExpression c)
        {
            string result = null;

            if (c.Value == null)
            {
                this.builder.Append(UriHelper.NULL);
                return(c);
            }
            else if (!ClientConvert.TryKeyPrimitiveToString(c.Value, out result))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, SR.ALinqCouldNotConvert, c.Value));
            }

            Debug.Assert(result != null, "result != null");

            // A Difference from WCF Data Services is that we will escape later when we execute the fully parsed query.
            this.builder.Append(result);
            return(c);
        }
Beispiel #4
0
        private string TypeNameForUri(Type type)
        {
            Debug.Assert(type != null, "type != null");
            type = Nullable.GetUnderlyingType(type) ?? type;

            if (ClientConvert.IsKnownType(type))
            {
                if (ClientConvert.IsSupportedPrimitiveTypeForUri(type))
                {
                    return(ClientConvert.ToTypeName(type));
                }

                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, SR.ALinqCantCastToUnsupportedPrimitive, type.Name));
            }
            else
            {
                return(null);

                // return this.context.ResolveNameFromType(type) ?? type.FullName;
            }
        }
Beispiel #5
0
            internal override Expression VisitUnary(UnaryExpression u)
            {
                Debug.Assert(u != null, "u != null");

                if (ResourceBinder.PatternRules.MatchConvertToAssignable(u))
                {
                    return(base.VisitUnary(u));
                }

                if ((u.NodeType == ExpressionType.Convert) || (u.NodeType == ExpressionType.ConvertChecked))
                {
                    Type sourceType = Nullable.GetUnderlyingType(u.Operand.Type) ?? u.Operand.Type;
                    Type targetType = Nullable.GetUnderlyingType(u.Type) ?? u.Type;

                    if (ClientConvert.IsKnownType(sourceType) && ClientConvert.IsKnownType(targetType))
                    {
                        return(base.Visit(u.Operand));
                    }
                }

                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, SR.ALinqExpressionNotSupportedInProjectionToEntity, this.type, u.ToString()));
            }
Beispiel #6
0
 internal static bool IsSupportedPrimitiveTypeForUri(Type type)
 {
     return(ClientConvert.ContainsReference(NamedTypesMap.Values.ToArray(), type));
 }
Beispiel #7
0
        internal static object ChangeType(string propertyValue, Type propertyType)
        {
            // Debug.Assert(null != propertyValue, "should never be passed null");
            try
            {
                switch ((StorageType)IndexOfStorage(propertyType))
                {
                case StorageType.Boolean:
                    return(XmlConvert.ToBoolean(propertyValue));

                case StorageType.Byte:
                    return(XmlConvert.ToByte(propertyValue));

                case StorageType.ByteArray:
                    return(Convert.FromBase64String(propertyValue));

                case StorageType.Char:
                    return(XmlConvert.ToChar(propertyValue));

                case StorageType.CharArray:
                    return(propertyValue.ToCharArray());

                case StorageType.DateTime:
                    return(XmlConvert.ToDateTime(propertyValue, XmlDateTimeSerializationMode.RoundtripKind));

                case StorageType.DateTimeOffset:
                    return(XmlConvert.ToDateTimeOffset(propertyValue));

                case StorageType.Decimal:
                    return(XmlConvert.ToDecimal(propertyValue));

                case StorageType.Double:
                    return(XmlConvert.ToDouble(propertyValue));

                case StorageType.Guid:
                    return(new Guid(propertyValue));

                case StorageType.Int16:
                    return(XmlConvert.ToInt16(propertyValue));

                case StorageType.Int32:
                    return(XmlConvert.ToInt32(propertyValue));

                case StorageType.Int64:
                    return(XmlConvert.ToInt64(propertyValue));

                case StorageType.Single:
                    return(XmlConvert.ToSingle(propertyValue));

                case StorageType.String:
                    return(propertyValue);

                case StorageType.SByte:
                    return(XmlConvert.ToSByte(propertyValue));

                case StorageType.TimeSpan:
                    return(XmlConvert.ToTimeSpan(propertyValue));

                case StorageType.Type:
                    return(Type.GetType(propertyValue, true));

                case StorageType.UInt16:
                    return(XmlConvert.ToUInt16(propertyValue));

                case StorageType.UInt32:
                    return(XmlConvert.ToUInt32(propertyValue));

                case StorageType.UInt64:
                    return(XmlConvert.ToUInt64(propertyValue));

                case StorageType.Uri:
                    return(ClientConvert.CreateUri(propertyValue, UriKind.RelativeOrAbsolute));

                case StorageType.XDocument:
                    return(0 < propertyValue.Length ? System.Xml.Linq.XDocument.Parse(propertyValue) : new System.Xml.Linq.XDocument());

                case StorageType.XElement:
                    return(System.Xml.Linq.XElement.Parse(propertyValue));

#if !ASTORIA_LIGHT
                case StorageType.Binary:
                    // Debug.Assert(null != KnownTypes[(int)StorageType.Binary], "null typeof(System.Data.Linq.Binary)");
                    return(Activator.CreateInstance(KnownTypes[(int)StorageType.Binary], Convert.FromBase64String(propertyValue)));
#endif
                default:
                    // Debug.Assert(false, "new StorageType without update to knownTypes");
                    return(propertyValue);
                }
            }
            catch (FormatException ex)
            {
                propertyValue = 0 == propertyValue.Length ? "String.Empty" : "String";
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "The current value '{1}' type is not compatible with the expected '{0}' type.", propertyType.ToString(), propertyValue), ex);
            }
            catch (OverflowException ex)
            {
                propertyValue = 0 == propertyValue.Length ? "String.Empty" : "String";
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "The current value '{1}' type is not compatible with the expected '{0}' type.", propertyType.ToString(), propertyValue), ex); // Strings.Deserialize_Current(propertyType.ToString(), propertyValue), ex);
            }
        }
Beispiel #8
0
        internal static bool TryXmlPrimitiveToString(object value, out string result)
        {
            Debug.Assert(value != null, "value != null");
            result = null;

            Type valueType = value.GetType();

            valueType = Nullable.GetUnderlyingType(valueType) ?? valueType;

            if (typeof(string) == valueType)
            {
                result = (string)value;
            }
            else if (typeof(bool) == valueType)
            {
                result = XmlConvert.ToString((bool)value);
            }
            else if (typeof(byte) == valueType)
            {
                result = XmlConvert.ToString((byte)value);
            }
            else if (typeof(DateTime) == valueType)
            {
                result = XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.RoundtripKind);
            }
            else if (typeof(decimal) == valueType)
            {
                result = XmlConvert.ToString((decimal)value);
            }
            else if (typeof(double) == valueType)
            {
                result = XmlConvert.ToString((double)value);
            }
            else if (typeof(Guid) == valueType)
            {
                result = value.ToString();
            }
            else if (typeof(short) == valueType)
            {
                result = XmlConvert.ToString((short)value);
            }
            else if (typeof(int) == valueType)
            {
                result = XmlConvert.ToString((int)value);
            }
            else if (typeof(long) == valueType)
            {
                result = XmlConvert.ToString((long)value);
            }
            else if (typeof(sbyte) == valueType)
            {
                result = XmlConvert.ToString((sbyte)value);
            }
            else if (typeof(float) == valueType)
            {
                result = XmlConvert.ToString((float)value);
            }
            else if (typeof(byte[]) == valueType)
            {
                byte[] byteArray = (byte[])value;
                result = Convert.ToBase64String(byteArray);
            }
#if ASTORIA_SERVER
            else if (typeof(System.Data.Linq.Binary) == valueType)
            {
                return(TryXmlPrimitiveToString(((System.Data.Linq.Binary)value).ToArray(), out result));
            }
#else
#if !ASTORIA_LIGHT
            else if (ClientConvert.IsBinaryValue(value))
            {
                return(ClientConvert.TryKeyBinaryToString(value, out result));
            }
#endif
#endif
            else if (typeof(System.Xml.Linq.XElement) == valueType)
            {
                result = ((System.Xml.Linq.XElement)value).ToString(System.Xml.Linq.SaveOptions.None);
            }
            else
            {
                result = null;
                return(false);
            }

            Debug.Assert(result != null, "result != null");
            return(true);
        }