Beispiel #1
0
        private static object ToObject(this IDictionary <string, object> source, ITypeCache typeCache, Type type, string dynamicPropertiesContainerName, bool dynamicObject)
        {
            if (typeCache == null)
            {
                throw new ArgumentNullException(nameof(typeCache));
            }

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

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

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

            // TODO: Should be a method on TypeCache
            if (CustomConverters.HasDictionaryConverter(type))
            {
                return(CustomConverters.Convert(source, type));
            }

            var instance = CreateInstance(type);

            IDictionary <string, object> dynamicProperties = null;

            if (!string.IsNullOrEmpty(dynamicPropertiesContainerName))
            {
                dynamicProperties = CreateDynamicPropertiesContainer(type, typeCache, instance, dynamicPropertiesContainerName);
            }

            foreach (var item in source)
            {
                var property = FindMatchingProperty(type, typeCache, item);

                if (property != null && property.CanWrite)
                {
                    if (item.Value != null)
                    {
                        property.SetValue(instance, ConvertValue(property.PropertyType, typeCache, item.Value), null);
                    }
                }
                else
                {
                    dynamicProperties?.Add(item.Key, item.Value);
                }
            }

            return(instance);
        }
        public static object ToObject(this IDictionary <string, object> source, Type type, string dynamicPropertiesContainerName = null)
        {
            var ctor = type.GetDefaultConstructor();

            if (ctor == null && !CustomConverters.HasDictionaryConverter(type))
            {
                throw new InvalidOperationException(
                          string.Format("Unable to create an instance of type {0} that does not have a default constructor.", type.Name));
            }

            return(ToObject(source, type, () => ctor.Invoke(new object[] { }), dynamicPropertiesContainerName, false));
        }
        private static object ToObject(this IDictionary <string, object> source, Type type, Func <object> instanceFactory,
                                       string dynamicPropertiesContainerName, bool dynamicObject)
        {
            if (source == null)
            {
                return(null);
            }
            if (typeof(IDictionary <string, object>).IsTypeAssignableFrom(type))
            {
                return(source);
            }
            if (type == typeof(ODataEntry))
            {
                return(CreateODataEntry(source, dynamicObject));
            }

            if (CustomConverters.HasDictionaryConverter(type))
            {
                return(CustomConverters.Convert(source, type));
            }

            var instance = instanceFactory();

            IDictionary <string, object> dynamicProperties = null;

            if (!string.IsNullOrEmpty(dynamicPropertiesContainerName))
            {
                dynamicProperties = CreateDynamicPropertiesContainer(type, instance, dynamicPropertiesContainerName);
            }

            foreach (var item in source)
            {
                var property = FindMatchingProperty(type, item);

                if (property != null && property.CanWrite && !property.IsNotMapped())
                {
                    if (item.Value != null)
                    {
                        property.SetValue(instance, ConvertValue(property.PropertyType, item.Value), null);
                    }
                }
                else if (dynamicProperties != null)
                {
                    dynamicProperties.Add(item.Key, item.Value);
                }
            }

            return(instance);
        }