Esempio n. 1
0
        public static T ToObject <T>(this IDictionary <string, object> source, ITypeCache typeCache, bool dynamicObject = false)
            where T : class
        {
            if (typeCache == null)
            {
                throw new ArgumentNullException(nameof(typeCache));
            }

            if (source == null)
            {
                return(default(T));
            }

            if (typeCache.IsTypeAssignableFrom(typeof(IDictionary <string, object>), typeof(T)))
            {
                return(source as T);
            }

            if (typeof(T) == typeof(ODataEntry))
            {
                return(CreateODataEntry(source, typeCache, dynamicObject) as T);
            }

            if (typeof(T) == typeof(string) || typeCache.IsValue(typeof(T)))
            {
                throw new InvalidOperationException($"Unable to convert structural data to {typeof(T).Name}.");
            }

            return((T)ToObject(source, typeCache, typeof(T), dynamicObject));
        }
Esempio n. 2
0
 public static bool TryConvert(this ITypeCache typeCache, object value, Type targetType, out object result)
 {
     try
     {
         if (value == null)
         {
             result = typeCache.IsValue(targetType) ? Activator.CreateInstance(targetType) : null;
         }
         else if (typeCache.IsTypeAssignableFrom(targetType, value.GetType()))
         {
             result = value;
         }
         else if (targetType == typeof(string))
         {
             result = value.ToString();
         }
         else if (typeCache.IsEnumType(targetType) && value is string)
         {
             result = Enum.Parse(targetType, value.ToString(), true);
         }
         else if (targetType == typeof(byte[]) && value is string)
         {
             result = System.Convert.FromBase64String(value.ToString());
         }
         else if (targetType == typeof(string) && value is byte[] bytes)
         {
             result = System.Convert.ToBase64String(bytes);
         }
         else if ((targetType == typeof(DateTime) || targetType == typeof(DateTime?)) && value is DateTimeOffset offset)
         {
             result = offset.DateTime;
         }
         else if ((targetType == typeof(DateTimeOffset) || targetType == typeof(DateTimeOffset?)) && value is DateTime time)
         {
             result = new DateTimeOffset(time);
         }
         else if (typeCache.IsEnumType(targetType))
         {
             result = Enum.ToObject(targetType, value);
         }
         else if (targetType == typeof(Guid) && value is string)
         {
             result = new Guid(value.ToString());
         }
         else if (Nullable.GetUnderlyingType(targetType) != null)
         {
             result = typeCache.Convert(value, Nullable.GetUnderlyingType(targetType));
         }
         else
         {
             result = System.Convert.ChangeType(value, targetType, CultureInfo.InvariantCulture);
         }
         return(true);
     }
     catch (Exception)
     {
         result = null;
         return(false);
     }
 }
        public static object Convert(this ITypeCache typeCache, object value, Type targetType)
        {
            if (value == null && !typeCache.IsValue(targetType))
            {
                return(null);
            }
            else if (typeCache.TryConvert(value, targetType, out var result))
            {
                return(result);
            }

            throw new FormatException($"Unable to convert value from type {value.GetType()} to type {targetType}");
        }
Esempio n. 4
0
 private static bool IsCompoundType(Type type, ITypeCache typeCache)
 {
     return(!typeCache.IsValue(type) && !type.IsArray && type != typeof(string));
 }