Esempio n. 1
0
        // Primitive <--> Value type
        private static bool TryFromPrimitive(Type targetType, Primitive value, out object output)
        {
            if (value.Value == null)
            {
                output = null;
                return(true);
            }

            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);
        }
Esempio n. 2
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));
            }
        }
Esempio n. 3
0
 // Primitive <--> Value type
 private static bool TryFromPrimitive(Type targetType, Primitive value, out object output)
 {
     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
     {
         output = null;
         return(false);
     }
     return(true);
 }