コード例 #1
0
        private object AsClrValue(IEdmValue edmValue, Type clrType, bool convertEnumValues)
        {
            TypeCode typeCode = PlatformHelper.GetTypeCode(clrType);

            if (typeCode == TypeCode.Object)
            {
                // First look for nullable primitives, then DateTime, DateTimeOffset and byte[] which don't have dedicated TypeCode, so they are processed here.
                if (clrType.IsGenericType() && clrType.GetGenericTypeDefinition() == TypeNullableOfT)
                {
                    if (edmValue is IEdmNullValue)
                    {
                        return(null);
                    }

                    return(this.AsClrValue(edmValue, clrType.GetGenericArguments().Single()));
                }
                else if (clrType == typeof(DateTime))
                {
                    return(AsClrDateTime(edmValue));
                }
                else if (clrType == typeof(DateTimeOffset))
                {
                    return(AsClrDateTimeOffset(edmValue));
                }
                else if (clrType == typeof(TimeSpan))
                {
                    return(AsClrTime(edmValue));
                }
                else if (clrType == typeof(byte[]))
                {
                    return(AsClrByteArray(edmValue));
                }
                else if (clrType.IsGenericType() && clrType.IsInterface() &&
                         (clrType.GetGenericTypeDefinition() == TypeICollectionOfT ||
                          clrType.GetGenericTypeDefinition() == TypeIListOfT ||
                          clrType.GetGenericTypeDefinition() == TypeIEnumerableOfT))
                {
                    // We are asked to produce an IEnumerable<T>, perform an equivalent of this.AsIEnumerable(edmValue, typeof(T)).Cast<T>().ToList()
                    return(this.AsListOfT(edmValue, clrType));
                }
                else
                {
                    return(this.AsClrObject(edmValue, clrType));
                }
            }
            else
            {
                // A CLR enum type will report some primitive type code, so we get here.
                // If this is the case and the value is of an edm enumeration type, we want to unbox the primitive type value,
                // otherwise assume the edm value is primitive and let it fail inside the converter if it's not.
                bool isEnum = clrType.IsEnum();
                if (isEnum)
                {
                    IEdmEnumValue edmEnumValue = edmValue as IEdmEnumValue;
                    if (edmEnumValue != null)
                    {
                        edmValue = edmEnumValue.Value;
                    }
                }

                object clrValue;
                if (!TryConvertAsPrimitiveType(PlatformHelper.GetTypeCode(clrType), edmValue, out clrValue))
                {
                    throw new InvalidCastException(Strings.EdmToClr_UnsupportedTypeCode(typeCode));
                }

                // In case of enums, because the converter returns a primitive type value we want to convert it to the CLR enum type.
                return((isEnum && convertEnumValues) ? this.GetEnumValue(clrValue, clrType) : clrValue);
            }
        }
コード例 #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);
                    }
                }
            }
        }