Ejemplo n.º 1
0
        public override PropertyTreeMetaObject BindInitializer(Expression expression, IExpressionContext context, IServiceProvider serviceProvider)
        {
            var values = expression.Evaluate(context);

            if (values == null)
            {
                return(this);
            }
            if (!(values is IEnumerable))
            {
                values = new [] { values };
            }

            var myValues = ReflectionMetaObject.GetAddonElements((IEnumerable)values);
            var addon    = ReflectionMetaObject.FindAddonMethod(ComponentType, myValues);

            if (addon == null)
            {
                var errors = serviceProvider.GetServiceOrDefault(PropertyTreeBinderErrors.Default);
                errors.NoAddMethodSupported(component.GetType(), PropertyTreeBinderImpl.FindFileLocation(serviceProvider));
                return(this);
            }
            foreach (var item in myValues)
            {
                addon.Invoke(Component, new object[] {
                    item
                });
            }
            return(this);
        }
Ejemplo n.º 2
0
        public override PropertyTreeMetaObject BindStreamingSource(StreamContext input, IServiceProvider serviceProvider)
        {
            var binder = serviceProvider.GetRequiredService <PropertyTreeMetaObjectBinder>();
            var ss     = binder.CreateStreamingSource(this, input, serviceProvider);

            if (ss == null)
            {
                var errors = serviceProvider.GetServiceOrDefault(PropertyTreeBinderErrors.Default);
                errors.CouldNotBindStreamingSource(this.ComponentType, PropertyTreeBinderImpl.FindFileLocation(serviceProvider));
                return(this);
            }

            // Hydrate the existing instance
            ss.Load(input, this.Component);
            return(this);
        }
Ejemplo n.º 3
0
        private bool TryAggregation(PropertyTreeMetaObject value,
                                    QualifiedName name,
                                    PropertyDefinition property,
                                    IServiceProvider serviceProvider)
        {
            object current;

            if (!property.TryGetValue(component, null, name, out current))
            {
                return(false);
            }

            var enumerable = value.Component as IEnumerable;

            if (current != null && enumerable != null)
            {
                var items = enumerable;
                if (!ReferenceEquals(current, items) && enumerable.GetEnumerator().MoveNext())
                {
                    MethodInfo mi = FindAddonMethod(current.GetType(), enumerable);

                    if (mi == null)
                    {
                        // Error because aggregation will be needed on read-only properties
                        if (property.IsReadOnly)
                        {
                            var errors = serviceProvider.GetServiceOrDefault(PropertyTreeBinderErrors.Default);
                            errors.NoAddMethodSupported(component.GetType(), PropertyTreeBinderImpl.FindFileLocation(serviceProvider));
                        }
                        return(false);
                    }

                    foreach (var item in items)
                    {
                        mi.Invoke(current, new object[] { item });
                    }
                    // Success because aggregation was applied
                    return(true);
                }
            }
            return(false);
        }