Beispiel #1
0
        private static bool LinkDecoder <T>(IDecoderDescriptor <T> descriptor, BindingFlags bindings,
                                            IDictionary <Type, object> parents)
        {
            var entityType = typeof(T);

            parents[entityType] = descriptor;

            if (Linker.TryLinkDecoderAsValue(descriptor))
            {
                return(true);
            }

            // Bind descriptor as an array of target type is also array
            if (entityType.IsArray)
            {
                var element = entityType.GetElementType();
                var setter  = MethodResolver
                              .Create <Func <Setter <object[], IEnumerable <object> > > >(() =>
                                                                                          SetterGenerator.CreateFromEnumerable <object>())
                              .SetGenericArguments(element)
                              .Invoke(null);

                return(Linker.LinkDecoderAsArray(descriptor, bindings, element, setter, parents));
            }

            // Try to bind descriptor as an array if target type IEnumerable<>
            foreach (var interfaceType in entityType.GetInterfaces())
            {
                // Make sure that interface is IEnumerable<T> and store typeof(T)
                if (!TypeResolver.Create(interfaceType)
                    .HasSameDefinitionThan <IEnumerable <object> >(out var interfaceTypeArguments))
                {
                    continue;
                }

                var elementType = interfaceTypeArguments[0];

                // Search constructor compatible with IEnumerable<>
                foreach (var constructor in entityType.GetConstructors())
                {
                    var parameters = constructor.GetParameters();

                    if (parameters.Length != 1)
                    {
                        continue;
                    }

                    var parameterType = parameters[0].ParameterType;

                    if (!TypeResolver.Create(parameterType)
                        .HasSameDefinitionThan <IEnumerable <object> >(out var parameterArguments) ||
                        parameterArguments[0] != elementType)
                    {
                        continue;
                    }

                    var setter = MethodResolver
                                 .Create <Func <ConstructorInfo, Setter <object, object> > >(c =>
                                                                                             SetterGenerator.CreateFromConstructor <object, object>(c))
                                 .SetGenericArguments(entityType, parameterType)
                                 .Invoke(null, constructor);

                    return(Linker.LinkDecoderAsArray(descriptor, bindings, elementType, setter, parents));
                }
            }

            // Bind readable and writable instance properties
            foreach (var property in entityType.GetProperties(bindings))
            {
                if (property.GetGetMethod() == null || property.GetSetMethod() == null ||
                    property.Attributes.HasFlag(PropertyAttributes.SpecialName))
                {
                    continue;
                }

                var setter = MethodResolver
                             .Create <Func <PropertyInfo, Setter <object, object> > >(p =>
                                                                                      SetterGenerator.CreateFromProperty <object, object>(p))
                             .SetGenericArguments(entityType, property.PropertyType)
                             .Invoke(null, property);

                if (!Linker.LinkDecoderAsObject(descriptor, bindings, property.PropertyType, property.Name, setter,
                                                parents))
                {
                    return(false);
                }
            }

            // Bind public instance fields
            foreach (var field in entityType.GetFields(bindings))
            {
                if (field.Attributes.HasFlag(FieldAttributes.SpecialName))
                {
                    continue;
                }

                var setter = MethodResolver
                             .Create <Func <FieldInfo, Setter <object, object> > >(f =>
                                                                                   SetterGenerator.CreateFromField <object, object>(f))
                             .SetGenericArguments(entityType, field.FieldType)
                             .Invoke(null, field);

                if (!Linker.LinkDecoderAsObject(descriptor, bindings, field.FieldType, field.Name, setter, parents))
                {
                    return(false);
                }
            }

            return(true);
        }