Example #1
0
        private static bool TryConvertAsPrimitiveType(TypeCode typeCode, IEdmValue edmValue, out object clrValue)
        {
            TypeCode typeCode1 = typeCode;

            switch (typeCode1)
            {
            case TypeCode.Boolean:
            {
                clrValue = EdmToClrConverter.AsClrBoolean(edmValue);
                return(true);
            }

            case TypeCode.Char:
            {
                clrValue = EdmToClrConverter.AsClrChar(edmValue);
                return(true);
            }

            case TypeCode.SByte:
            {
                clrValue = (sbyte)EdmToClrConverter.AsClrInt64(edmValue);
                return(true);
            }

            case TypeCode.Byte:
            {
                clrValue = EdmToClrConverter.AsClrByte(edmValue);
                return(true);
            }

            case TypeCode.Int16:
            {
                clrValue = EdmToClrConverter.AsClrInt16(edmValue);
                return(true);
            }

            case TypeCode.UInt16:
            {
                clrValue = (ushort)EdmToClrConverter.AsClrInt64(edmValue);
                return(true);
            }

            case TypeCode.Int32:
            {
                clrValue = EdmToClrConverter.AsClrInt32(edmValue);
                return(true);
            }

            case TypeCode.UInt32:
            {
                clrValue = (uint)EdmToClrConverter.AsClrInt64(edmValue);
                return(true);
            }

            case TypeCode.Int64:
            {
                clrValue = EdmToClrConverter.AsClrInt64(edmValue);
                return(true);
            }

            case TypeCode.UInt64:
            {
                clrValue = (ulong)EdmToClrConverter.AsClrInt64(edmValue);
                return(true);
            }

            case TypeCode.Single:
            {
                clrValue = EdmToClrConverter.AsClrSingle(edmValue);
                return(true);
            }

            case TypeCode.Double:
            {
                clrValue = EdmToClrConverter.AsClrDouble(edmValue);
                return(true);
            }

            case TypeCode.Decimal:
            {
                clrValue = EdmToClrConverter.AsClrDecimal(edmValue);
                return(true);
            }

            case TypeCode.DateTime:
            {
                clrValue = EdmToClrConverter.AsClrDateTime(edmValue);
                return(true);
            }

            case TypeCode.Object | TypeCode.DateTime:
            {
                clrValue = null;
                return(false);
            }

            case TypeCode.String:
            {
                clrValue = EdmToClrConverter.AsClrString(edmValue);
                return(true);
            }

            default:
            {
                clrValue = null;
                return(false);
            }
            }
        }
Example #2
0
        private object AsClrValue(IEdmValue edmValue, Type clrType, bool convertEnumValues)
        {
            object   obj      = null;
            TypeCode typeCode = PlatformHelper.GetTypeCode(clrType);

            if (typeCode != TypeCode.Object)
            {
                bool flag = clrType.IsEnum();
                if (flag)
                {
                    IEdmEnumValue edmEnumValue = edmValue as IEdmEnumValue;
                    if (edmEnumValue != null)
                    {
                        edmValue = edmEnumValue.Value;
                    }
                }
                if (EdmToClrConverter.TryConvertAsPrimitiveType(PlatformHelper.GetTypeCode(clrType), edmValue, out obj))
                {
                    if (!flag || !convertEnumValues)
                    {
                        return(obj);
                    }
                    else
                    {
                        return(this.GetEnumValue(obj, clrType));
                    }
                }
                else
                {
                    throw new InvalidCastException(Strings.EdmToClr_UnsupportedTypeCode(typeCode));
                }
            }
            else
            {
                if (!clrType.IsGenericType() || !(clrType.GetGenericTypeDefinition() == EdmToClrConverter.TypeNullableOfT))
                {
                    if (clrType != typeof(DateTime))
                    {
                        if (clrType != typeof(DateTimeOffset))
                        {
                            if (clrType != typeof(TimeSpan))
                            {
                                if (clrType != typeof(byte[]))
                                {
                                    if (!clrType.IsGenericType() || !clrType.IsInterface() || !(clrType.GetGenericTypeDefinition() == EdmToClrConverter.TypeICollectionOfT) && !(clrType.GetGenericTypeDefinition() == EdmToClrConverter.TypeIListOfT) && !(clrType.GetGenericTypeDefinition() == EdmToClrConverter.TypeIEnumerableOfT))
                                    {
                                        return(this.AsClrObject(edmValue, clrType));
                                    }
                                    else
                                    {
                                        return(this.AsListOfT(edmValue, clrType));
                                    }
                                }
                                else
                                {
                                    return(EdmToClrConverter.AsClrByteArray(edmValue));
                                }
                            }
                            else
                            {
                                return(EdmToClrConverter.AsClrTime(edmValue));
                            }
                        }
                        else
                        {
                            return(EdmToClrConverter.AsClrDateTimeOffset(edmValue));
                        }
                    }
                    else
                    {
                        return(EdmToClrConverter.AsClrDateTime(edmValue));
                    }
                }
                else
                {
                    if (edmValue as IEdmNullValue == null)
                    {
                        return(this.AsClrValue(edmValue, clrType.GetGenericArguments().Single <Type>()));
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
        }