Ejemplo n.º 1
0
        protected override Expression VisitMember(MemberExpression node)
        {
            var memberName = _typeCache.GetMappedName(node.Expression.Type, node.Member.Name);

            ExpandAssociations.AddRange(node.Expression is MemberExpression
                ? ExtractNestedExpandAssociations(node.Expression)
                : new[] { new ODataExpandAssociation(memberName) });

            return(node);
        }
        private static IEnumerable <string> ExtractColumnNames(MemberExpression memberExpression, ITypeCache typeCache)
        {
            var memberName = typeCache.GetMappedName(memberExpression.Expression.Type, memberExpression.Member.Name);

            //var memberName = memberExpression.Expression.Type
            //    .GetNamedProperty(memberExpression.Member.Name)
            //    .GetMappedName();

            return(memberExpression.Expression is MemberExpression
                ? memberExpression.Expression.ExtractColumnNames(typeCache).Select(x => string.Join("/", x, memberName))
                : new[] { memberName });
        }
        protected override Expression VisitMember(MemberExpression node)
        {
            var memberName            = _typeCache.GetMappedName(node.Expression.Type, node.Member.Name);
            var association           = new ODataExpandAssociation(memberName);
            var associationCollection = node.Expression is MemberExpression
                ? AddNestedExpandAssociationAndGetDeepestChild(node.Expression).ExpandAssociations
                : ExpandAssociations;

            associationCollection.Add(association);

            return(node);
        }
Ejemplo n.º 4
0
        private static Type GetTypeFromAnnotation(IDictionary <string, object> source, ITypeCache typeCache, Type type)
        {
            var annotations = source[FluentCommand.AnnotationsLiteral] as ODataEntryAnnotations;

            var odataType = annotations?.TypeName;

            if (string.IsNullOrEmpty(odataType))
            {
                return(type);
            }

            // 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)
                {
                    return(derived);
                }

                foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    var typeFound = assembly.GetType(odataType);
                    if (typeFound != null)
                    {
                        return(typeFound);
                    }
                }

                // TODO: Should we throw an exception here or log a warning here as we don't understand the data
            }

            return(type);
        }
        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);
        }