예제 #1
0
        public static object ToObject(this IDictionary <string, object> source, ITypeCache typeCache, Type type, bool dynamicObject = false)
        {
            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));
            }

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

            var instance = CreateInstance(type);

            IDictionary <string, object> dynamicProperties = null;
            var dynamicPropertiesContainerName             = typeCache.DynamicContainerName(type);

            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);
        }
예제 #2
0
        public static object ToObject(this IDictionary <string, object> source, ITypeCache typeCache, Type type, bool dynamicObject = false)
        {
            if (typeCache == null)
            {
                throw new ArgumentNullException(nameof(typeCache));
            }

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

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

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

            // Check before custom converter so we use the most appropriate type.
            if (source.ContainsKey(FluentCommand.AnnotationsLiteral))
            {
                type = GetTypeFromAnnotation(source, typeCache, type);
            }

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

            if (type.HasCustomAttribute(typeof(CompilerGeneratedAttribute), false))
            {
                return(CreateInstanceOfAnonymousType(source, type, typeCache));
            }

            var instance = CreateInstance(type);

            IDictionary <string, object> dynamicProperties = null;
            var dynamicPropertiesContainerName             = typeCache.DynamicContainerName(type);

            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, ITypeCache typeCache, Type type, bool dynamicObject = false)
        {
            if (typeCache == null)
            {
                throw new ArgumentNullException(nameof(typeCache));
            }

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

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

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

            // Check before custom converter so we use the most appropriate type.
            if (source.ContainsKey(FluentCommand.AnnotationsLiteral))
            {
                var annotations = source[FluentCommand.AnnotationsLiteral] as ODataEntryAnnotations;
                var odataType   = annotations?.TypeName;
                if (!string.IsNullOrEmpty(odataType))
                {
                    // TODO: We could cast the ITypeCatcher to TypeCache and use it's property but it's a bit naughty - conditional?
                    var resolver = ODataNameMatchResolver.NotStrict;
                    if (!resolver.IsMatch(odataType, type.Name))
                    {
                        // Ok, something other than the base type, see if we can match it
                        var derived = typeCache.GetDerivedTypes(type).FirstOrDefault(x => resolver.IsMatch(odataType, typeCache.GetMappedName(x)));
                        if (derived != null)
                        {
                            // Use the derived type from now on
                            type = derived;
                        }
                        else
                        {
                            // TODO: Should we throw an exception here or log a warning here as we don't understand the data
                        }
                    }
                }
            }

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

            var instance = CreateInstance(type);

            IDictionary <string, object> dynamicProperties = null;
            var dynamicPropertiesContainerName             = typeCache.DynamicContainerName(type);

            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);
        }