private PropertyTreeMetaObject DoOperatorBind(PropertyTreeBinderImpl parent, PropertyTreeMetaObject target, PropertyTreeNavigator navigator, OperatorDefinition op)
            {
                OperatorDefinition addon = op;

                if (addon.DefaultParameter != null) {
                    Type itemType = ((PropertyTreeFactoryDefinition) addon).OutputType;
                    // TODO Use service activation (we have the output type)

                    var item = target.CreateChild(itemType);
                    var model = parent.Bind(item, navigator, null);
                    var args = new Dictionary<string, PropertyTreeMetaObject>
                    {
                        { addon.DefaultParameter.Name, model }
                    };

                    try {
                        target.BindAddChild(op, args);

                    } catch (Exception ex) {
                        if (ex.IsCriticalException())
                            throw;

                        parent.errors.BadAddChild(target.ComponentType, ex, navigator.FileLocation);
                    }

                } else {
                    // TODO The number and kinds of arguments are constrained.  This should probably
                    // be enforced within schema operator reflection (spec)

                    Action<IReadOnlyDictionary<string, PropertyTreeMetaObject>> func;
                    var children = NodeList.Create(SelectChildren(navigator));

                    switch (addon.OperatorType) {
                        case OperatorType.Add:
                            func = args => {
                                var child = target.BindAddChild(addon, args);

                                if (child.ShouldBindChildren) {
                                    parent.BindChildNodes(child, navigator, children);
                                }
                            };
                            break;

                        case OperatorType.Remove:
                            func = args => target.BindRemoveChild(addon, args);
                            break;

                        case OperatorType.Clear:
                        default:
                            func = args => target.BindClearChildren(addon, args);
                            break;
                    }

                    var services = parent.GetBasicServices(navigator);
                    var args2 = parent.ExtractParameterDictionary(op, target, services, children);
                    func(args2);
                }

                return target;
            }
        private void DoPropertyBind(PropertyTreeMetaObject target,
                                    PropertyTreeNavigator navigator,
                                    PropertyDefinition property)
        {
            object ancestor = null;
            PropertyTreeMetaObject ancestorMeta = null;

            if (property.IsExtender)
            {
                var ancestorType = property.DeclaringTreeDefinition.SourceClrType;
                ancestorMeta = target.Ancestors.FirstOrDefault(
                    t => ancestorType.IsAssignableFrom(t.ComponentType));

                if (ancestorMeta != null)
                {
                    ancestor = ancestorMeta.Component;
                }
            }

            var component = target.Component;
            PropertyTreeMetaObject propertyTarget = target.CreateChild(property, navigator.QualifiedName, ancestorMeta);

            var services = new PropertyBindContext(
                component,
                property,
                ServiceProvider.Compose(ServiceProvider.FromValue(navigator), this))
            {
                LineNumber   = navigator.LineNumber,
                LinePosition = navigator.LinePosition,
            };

            try {
                propertyTarget = Bind(propertyTarget, navigator, services);
                target.BindSetMember(property, navigator.QualifiedName, propertyTarget, ancestorMeta, services);
            } catch (NullReferenceException nre) {
                // Normally a "critical" exception, consider it a conversion error
                _errors.BinderConversionError(property.Name, target.ComponentType, nre, navigator.FileLocation);
            } catch (ArgumentException a) {
                _errors.BinderConversionError(property.Name, target.ComponentType, a, navigator.FileLocation);
            } catch (PropertyTreeException) {
                throw;
            } catch (Exception ex) {
                if (ex.IsCriticalException())
                {
                    throw;
                }
                _errors.BinderConversionError(property.Name, target.ComponentType, ex, navigator.FileLocation);
            }
        }
예제 #3
0
        public Dictionary <string, PropertyTreeMetaObject> ExtractParameterDictionary(
            OperatorDefinition op,
            PropertyTreeMetaObject target,
            IServiceProvider serviceProvider,
            NodeList children)
        {
            // Named constructor arguments
            var duplicates = new HashSet <QualifiedName>();
            var mapped     = new Dictionary <QualifiedName, PropertyTreeNavigator>();

            foreach (var child in children)
            {
                // Implicitly map default NS to real
                var impliedName = ImpliedName(child, target);

                if (duplicates.Contains(impliedName))
                {
                    // Duplicates can't bind to parameters (only to param arrays)
                }
                else if (mapped.ContainsKey(impliedName))
                {
                    // Detected a duplicate
                    duplicates.Add(impliedName);
                    mapped.Remove(impliedName);
                }
                else
                {
                    mapped.Add(impliedName, child);
                }
            }

            var args = new Dictionary <string, PropertyTreeMetaObject>(op.Parameters.Count);

            PropertyDefinition myParam         = null;
            List <string>      requiredMissing = new List <string>();

            foreach (PropertyDefinition p in op.Parameters)
            {
                // Fallback to empty ns
                PropertyTreeNavigator nav;
                QualifiedName         impliedName = p.QualifiedName;
                if (p.QualifiedName.Namespace.IsDefault)
                {
                    impliedName = impliedName.ChangeNamespace(op.Namespace);
                }

                if (mapped.TryGetValue(impliedName, out nav))
                {
                    // Binds a parameter required for activating an instance
                    // TODO Should we supply/use attributes from the parameter
                    // and/or corresponding property descriptor?

                    var childContext = target.CreateChild(p.PropertyType);
                    args[p.Name] = Bind(childContext, nav, serviceProvider);
                    children.Remove(nav);
                }
                else if (p.IsOptional)
                {
                    PropertyTreeMetaObject defaultValue;
                    if (p.DefaultValue == null)
                    {
                        defaultValue = PropertyTreeMetaObject.Create(p.PropertyType);
                    }
                    else
                    {
                        defaultValue = PropertyTreeMetaObject.Create(p.DefaultValue);
                    }
                    args[p.Name] = defaultValue;
                }
                else if (p.IsParamArray)
                {
                    myParam = p;
                }

                else if (TypeHelper.IsParameterRequired(p.PropertyType))
                {
                    requiredMissing.Add(Utility.DisplayName(p.QualifiedName));
                }
            }

            if (requiredMissing.Count > 0)
            {
                _errors.RequiredPropertiesMissing(requiredMissing, op, FindFileLocation(serviceProvider));
            }

            if (myParam == null && !target.GetDefinition().DefaultProperties.Any() && duplicates.Any(t => target.SelectProperty(t) != null))
            {
                _errors.DuplicatePropertyName(duplicates, FindFileLocation(serviceProvider));
            }

            // Try param array
            if (myParam != null)
            {
                var all         = new List <object>();
                var elementType = myParam.PropertyType.GetElementType();
                foreach (var kvp in children)
                {
                    // Bind child nodes so tha latebound applies
                    var childrenList = NodeList.Create(PropertyTreeBinderImpl.SelectChildren(kvp));
                    var inline       = BindChildNodes(PropertyTreeMetaObject.Create(elementType), kvp, childrenList);
                    var inlineVal    = inline.Component;
                    all.Add(inlineVal);
                }

                children.Clear();
                var array = Array.CreateInstance(elementType, all.Count);
                ((System.Collections.ICollection)all).CopyTo(array, 0);
                args[myParam.Name] = PropertyTreeMetaObject.Create(array);
            }

            return(args);
        }
            private PropertyTreeMetaObject DoOperatorBind(PropertyTreeBinderImpl parent, PropertyTreeMetaObject target, PropertyTreeNavigator navigator, OperatorDefinition op)
            {
                OperatorDefinition addon = op;

                if (addon.DefaultParameter != null)
                {
                    Type itemType = addon.OutputType;
                    // TODO Use service activation (we have the output type)

                    var item  = target.CreateChild(itemType);
                    var model = parent.Bind(item, navigator, null);
                    var args  = new Dictionary <string, PropertyTreeMetaObject>
                    {
                        { addon.DefaultParameter.Name, model }
                    };

                    try {
                        target.BindAddChild(op, args);
                    } catch (PropertyTreeException) {
                        throw;
                    } catch (Exception ex) {
                        if (ex.IsCriticalException())
                        {
                            throw;
                        }

                        parent._errors.BadAddChild(target.ComponentType, ex, navigator.FileLocation);
                    }
                }
                else
                {
                    // TODO The number and kinds of arguments are constrained.  This should probably
                    // be enforced within schema operator reflection (spec)

                    Action <IReadOnlyDictionary <string, PropertyTreeMetaObject> > func;
                    var children = NodeList.Create(SelectChildren(navigator));

                    switch (addon.OperatorType)
                    {
                    case OperatorType.Add:
                        func = args => {
                            var child = target.BindAddChild(addon, args);

                            if (child.ShouldBindChildren)
                            {
                                parent.BindChildNodes(child, navigator, children);
                            }
                        };
                        break;

                    case OperatorType.Remove:
                        func = args => target.BindRemoveChild(addon, args);
                        break;

                    case OperatorType.Clear:
                    default:
                        func = args => target.BindClearChildren(addon, args);
                        break;
                    }

                    var services = parent.GetBasicServices(navigator);
                    var args2    = parent.ExtractParameterDictionary(op, target, services, children);
                    func(args2);
                }

                return(target);
            }
            private void DoPropertyBind(PropertyTreeBinderImpl parent,
                                        PropertyTreeMetaObject target,
                                        PropertyTreeNavigator navigator,
                                        PropertyDefinition property)
            {
                object ancestor = null;
                PropertyTreeMetaObject ancestorMeta = null;

                if (property.IsExtender) {
                    var ancestorType = property.DeclaringTreeDefinition.SourceClrType;
                    ancestorMeta = target.GetAncestors().Cast<PropertyTreeMetaObject>().FirstOrDefault(
                        t => ancestorType.IsAssignableFrom(t.ComponentType));

                    if (ancestorMeta != null)
                        ancestor = ancestorMeta.Component;
                }

                var component = target.Component;
                PropertyTreeMetaObject propertyTarget = target.CreateChild(property, navigator.QualifiedName, ancestorMeta);

                var services = new PropertyBindContext(
                    component,
                    property,
                    ServiceProvider.Compose(ServiceProvider.FromValue(navigator), parent))
                {
                    LineNumber = navigator.LineNumber,
                    LinePosition = navigator.LinePosition,
                };

                propertyTarget = parent.Bind(propertyTarget, navigator, services);
                target.BindSetMember(property, navigator.QualifiedName, propertyTarget, ancestorMeta, services);
            }
        public Dictionary<string, PropertyTreeMetaObject> ExtractParameterDictionary(
            OperatorDefinition op,
            PropertyTreeMetaObject target,
            IServiceProvider serviceProvider,
            NodeList children)
        {
            // Named constructor arguments
            var duplicates = new HashSet<QualifiedName>();
            var mapped = new Dictionary<QualifiedName, PropertyTreeNavigator>();
            foreach (var child in children) {
                // Implicitly map default NS to real
                var impliedName = ImpliedName(child, target);

                if (duplicates.Contains(impliedName)) {
                    // Duplicates can't bind to parameters (only to param arrays)

                } else if (mapped.ContainsKey(impliedName)) {
                    // Detected a duplicate
                    duplicates.Add(impliedName);
                    mapped.Remove(impliedName);

                } else {
                    mapped.Add(impliedName, child);
                }
            }

            var args = new Dictionary<string, PropertyTreeMetaObject>(op.Parameters.Count);

            PropertyDefinition myParam = null;
            List<string> requiredMissing = new List<string>();

            foreach (PropertyDefinition p in op.Parameters) {
                // Fallback to empty ns
                PropertyTreeNavigator nav;
                QualifiedName impliedName = p.QualifiedName;
                if (p.QualifiedName.Namespace.IsDefault) {
                    impliedName = impliedName.ChangeNamespace(op.Namespace);
                }

                if (mapped.TryGetValue(impliedName, out nav)) {
                    // Binds a parameter required for activating an instance
                    // TODO Should we supply/use attributes from the parameter
                    // and/or corresponding property descriptor?

                    var childContext = target.CreateChild(p.PropertyType);
                    args[p.Name] = Bind(childContext, nav, serviceProvider);
                    children.Remove(nav);
                }
                else if (p.IsOptional) {
                    PropertyTreeMetaObject defaultValue;
                    if (p.DefaultValue == null)
                        defaultValue = PropertyTreeMetaObject.Create(p.PropertyType);
                    else
                        defaultValue = PropertyTreeMetaObject.Create(p.DefaultValue);
                    args[p.Name] = defaultValue;

                } else if (p.IsParamArray)
                    myParam = p;

                else if (TypeHelper.IsParameterRequired(p.PropertyType)) {
                    requiredMissing.Add(Utility.DisplayName(p.QualifiedName));
                }
            }

            if (requiredMissing.Count > 0)
                errors.RequiredPropertiesMissing(requiredMissing, op, FindFileLocation(serviceProvider));

            if (myParam == null && target.GetDefinition().DefaultProperty == null && duplicates.Any(t => target.SelectProperty(t) != null))
                errors.DuplicatePropertyName(duplicates, FindFileLocation(serviceProvider));

            // Try param array
            if (myParam != null) {
                var all = new List<object>();
                var elementType = myParam.PropertyType.GetElementType();
                foreach (var kvp in children) {

                    // Bind child nodes so tha latebound applies
                    var childrenList = NodeList.Create(PropertyTreeBinderImpl.SelectChildren(kvp));
                    var inline = BindChildNodes(PropertyTreeMetaObject.Create(elementType), kvp, childrenList);
                    var inlineVal = inline.Component;
                    all.Add(inlineVal);
                }

                children.Clear();
                var array = Array.CreateInstance(elementType, all.Count);
                ((System.Collections.ICollection) all).CopyTo(array, 0);
                args[myParam.Name] = PropertyTreeMetaObject.Create(array);
            }

            return args;
        }