public static object PrimitiveToValue(Type type, Primitive primitive)
 {
     if (type == typeof(float))
     {
         return(primitive.AsSingle());
     }
     if (type == typeof(int))
     {
         return(primitive.AsInt());
     }
     if (type == typeof(string))
     {
         return(primitive.AsString());
     }
     if (type == typeof(long))
     {
         return(primitive.AsLong());
     }
     if (type == typeof(byte[]))
     {
         return(primitive.AsByteArray());
     }
     if (type.IsEnum)
     {
         return(Enum.Parse(type, primitive));
     }
     if (type == typeof(bool))
     {
         return(primitive.AsBoolean());
     }
     if (type == typeof(Guid))
     {
         return(primitive.AsGuid());
     }
     if (type == typeof(object))
     {
         return(primitive.Value);
     }
     throw new Exception(string.Format("Unsupported type {0}", type.Name));
 }
        // Primitive <--> Value type
        private static bool TryFromPrimitive(Type targetType, Primitive value, out object output)
        {
            if (value.Value == null)
            {
                output = null;
                return(true);
            }

            if (targetType.IsAssignableFrom(typeof(Boolean)))
            {
                output = value.AsBoolean();
            }
            else if (targetType.IsAssignableFrom(typeof(Byte)))
            {
                output = value.AsByte();
            }
            else if (targetType.IsAssignableFrom(typeof(Char)))
            {
                output = value.AsChar();
            }
            else if (targetType.IsAssignableFrom(typeof(DateTime)))
            {
                output = value.AsDateTime();
            }
            else if (targetType.IsAssignableFrom(typeof(Decimal)))
            {
                output = value.AsDecimal();
            }
            else if (targetType.IsAssignableFrom(typeof(Double)))
            {
                output = value.AsDouble();
            }
            else if (targetType.IsAssignableFrom(typeof(int)))
            {
                output = value.AsInt();
            }
            else if (targetType.IsAssignableFrom(typeof(long)))
            {
                output = value.AsLong();
            }
            else if (targetType.IsAssignableFrom(typeof(SByte)))
            {
                output = value.AsSByte();
            }
            else if (targetType.IsAssignableFrom(typeof(short)))
            {
                output = value.AsShort();
            }
            else if (targetType.IsAssignableFrom(typeof(Single)))
            {
                output = value.AsSingle();
            }
            else if (targetType.IsAssignableFrom(typeof(String)))
            {
                output = value.AsString();
            }
            else if (targetType.IsAssignableFrom(typeof(uint)))
            {
                output = value.AsUInt();
            }
            else if (targetType.IsAssignableFrom(typeof(ulong)))
            {
                output = value.AsULong();
            }
            else if (targetType.IsAssignableFrom(typeof(ushort)))
            {
                output = value.AsUShort();
            }
            else if (targetType.IsAssignableFrom(typeof(Guid)))
            {
                output = value.AsGuid();
            }
            else if (targetType.IsAssignableFrom(typeof(byte[])))
            {
                output = value.AsByteArray();
            }
            else if (targetType.IsAssignableFrom(typeof(MemoryStream)))
            {
                output = value.AsMemoryStream();
            }
            else
            {
                output = null;
                return(false);
            }
            return(true);
        }
Esempio n. 3
0
        // Primitive <--> Value type
        private static bool TryFromPrimitive(Type targetType, Primitive value, out object output)
        {
            if (value.Value == null)
            {
                output = null;
                return(true);
            }

            try
            {
                var targetTypeWrapper = TypeFactory.GetTypeInfo(targetType);
                if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(Boolean))))
                {
                    output = value.AsBoolean();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(Byte))))
                {
                    output = value.AsByte();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(Char))))
                {
                    output = value.AsChar();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(DateTime))))
                {
                    output = value.AsDateTime();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(Decimal))))
                {
                    output = value.AsDecimal();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(Double))))
                {
                    output = value.AsDouble();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(int))))
                {
                    output = value.AsInt();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(long))))
                {
                    output = value.AsLong();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(SByte))))
                {
                    output = value.AsSByte();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(short))))
                {
                    output = value.AsShort();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(Single))))
                {
                    output = value.AsSingle();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(String))))
                {
                    output = value.AsString();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(uint))))
                {
                    output = value.AsUInt();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(ulong))))
                {
                    output = value.AsULong();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(ushort))))
                {
                    output = value.AsUShort();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(Guid))))
                {
                    output = value.AsGuid();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(byte[]))))
                {
                    output = value.AsByteArray();
                }
                else if (targetTypeWrapper.IsAssignableFrom(TypeFactory.GetTypeInfo(typeof(MemoryStream))))
                {
                    output = value.AsMemoryStream();
                }
                else
                {
                    output = null;
                    return(false);
                }
                return(true);
            }
            catch (InvalidCastException)
            {
                throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture,
                                                             "Unable to cast value [{0}] of type [{1}] to type [{2}]",
                                                             value.Value,
                                                             value.Value.GetType().FullName,
                                                             targetType.FullName));
            }
        }