예제 #1
0
        private void ResolveAttributes(InitializerObject instance, XAttribute attribute)
        {
            string propertyname = attribute.Name.LocalName;

            if (attribute.IsNamespaceDeclaration && !_assemblyKeys.ContainsKey(propertyname))
            {
                if (NamespaceDeclaration.Resolve(propertyname, attribute.Value, out NamespaceDeclaration declaration))
                {
                    _assemblyKeys.Add(propertyname, declaration);
                }
            }
            else
            {
                int count = NestedArgument.ResolveNestedParenthesses(attribute.Value, '{', '}', out IEnumerable <NestedArgument> nestedargument);
                if (!instance.GetProperty(propertyname, out InitializerProperty property))
                {
                    _debug?.Pass(this, "initializer/instance/noproperty", (s) => string.Format(s, propertyname, instance, instance.GetType()));
                }
                else if (count > 1)
                {
                    _debug?.Pass(this, "initializer/format/chained", (s) => string.Format(s, attribute.Value));
                }
                else if (count == 1)
                {
                    NestedArgument argument = nestedargument.First();
                    if (ResolveExtension(argument, property.Type, out object obj))
                    {
                        if (obj is PropertyBinding binding)
                        {
                            property.Register(binding);
                        }
                        else if (obj != null)
                        {
                            property.SetValue(obj);
                        }
                    }
                    else
                    {
                        _debug?.Pass(this, "initializer/extension/failed", (s) => string.Format(argument.Scope));
                    }
                }
                else if (count == 0 && SaveChangeType(property.Type, attribute.Value, out object obj))
                {
                    property.SetValue(obj);
                }
            }
        }
예제 #2
0
        private void ResolveElements(InitializerObject instance, XElement parent, XElement node, ContentPropertyAttribute contentproperty)
        {
            string propertysource;

            string[] nodename = node.Name.LocalName.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            if (nodename.Length > 2)
            {
                //TODO: loggen
                return;
            }
            else if (nodename.Length == 2 && string.Equals(nodename[0], parent.Name.LocalName))
            {
                propertysource = nodename[1];
            }
            else if (contentproperty == null)
            {
                _debug?.Pass(this, "initializer/element/nocontent", (s) => string.Format(s, parent.Name));
                propertysource = null;
            }
            else
            {
                propertysource = contentproperty.Name;
            }
            if (!string.IsNullOrWhiteSpace(propertysource))
            {
                if (instance.GetProperty(propertysource, out InitializerProperty property))
                {
                    object propertyvalue = property.GetValue();
                    if (propertyvalue is IAddContent propertyinterface)
                    {
                        if (nodename.Length == 2)
                        {
                            foreach (XElement ele in node.Elements())
                            {
                                propertyinterface.Add(GetObject(ele, propertyinterface));
                            }
                        }
                        else
                        {
                            propertyinterface.Add(GetObject(node, instance));
                        }
                    }
                    else if (propertyvalue is ResourceContent)
                    {
                        InitializerObject proptertyobject = propertyvalue as InitializerObject;
                        if (contentproperty != null && propertysource == contentproperty.Name)
                        {
                            CallDictionaryAdd(node, instance, null, (n, v) => proptertyobject.Invoke(contentproperty.MethodName, out object res, v));
                        }
                        else
                        {
                            foreach (XElement ele in node.Elements())
                            {
                                CallDictionaryAdd(ele, instance, null, (n, v) =>
                                                  proptertyobject.Invoke(contentproperty.MethodName, out object res, v));
                            }
                        }
                    }
                    else if (propertyvalue is InitializerObject propertyobject)
                    {
                        if (nodename.Length == 2)
                        {
                            if (propertyobject.HasMethod(contentproperty.MethodName))
                            {
                                if (contentproperty != null && propertysource == contentproperty.Name)
                                {
                                    propertyobject.Invoke(contentproperty.MethodName, out object res, GetObject(node, instance));
                                }
                                else
                                {
                                    foreach (XElement ele in node.Elements())
                                    {
                                        propertyobject.Invoke(contentproperty.MethodName, out object res, GetObject(ele, instance));
                                    }
                                }
                            }
                            else
                            {
                                _debug?.Pass(this, "initializer/contentmethod/notfound", (s) => string.Format(s, propertyvalue, instance));
                            }
                        }
                        else if (!propertyobject.Invoke("Add", out object res, GetObject(node, instance)))
                        {
                            _debug?.Pass(this, "initializer/contentmethod/notfound", (s) => string.Format(s, propertyvalue, instance));
                        }
                    }
                    else if (property.Type.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary <,>) || i == typeof(IDictionary)))
                    {
                        MethodInfo method = property.Type.GetMethods().FirstOrDefault(m => m.Name.ToLower() == "add" && m.GetParameters().Count() == 2);
                        if (method == null)
                        {
                            _debug?.Pass(this, "initializer/contentmethod/notfound", (s) => string.Format(s, propertyvalue, instance));
                        }
                        else if (contentproperty != null && propertysource == contentproperty.Name)
                        {
                            CallDictionaryAdd(node, instance, propertyvalue, method.Invoke);
                        }
                        else
                        {
                            foreach (XElement ele in node.Elements())
                            {
                                CallDictionaryAdd(ele, instance, propertyvalue, method.Invoke);
                            }
                        }
                    }
                    else if (property.Type.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollection <>) || i == typeof(ICollection)))
                    {
                        MethodInfo method = property.Type.GetMethods().FirstOrDefault(m => m.Name.ToLower() == "add" && m.GetParameters().Count() == 1);
                        if (method == null)
                        {
                            _debug?.Pass(this, "initializer/contentmethod/notfound", (s) => string.Format(s, propertyvalue, instance));
                        }
                        else if (contentproperty != null && propertysource == contentproperty.Name)
                        {
                            method.Invoke(propertyvalue, new[] { GetObject(node, instance) });
                        }
                        else
                        {
                            foreach (XElement ele in node.Elements())
                            {
                                method.Invoke(propertyvalue, new[] { GetObject(ele, instance) });
                            }
                        }
                    }
                    else
                    {
                        //InitializerProperty initializerproperty;
                        if (node.HasElements)
                        {
                            instance.SetValue(propertysource, GetObject(node.Elements().FirstOrDefault(), instance));
                        }
                        else if (instance.GetProperty(nodename[1], out InitializerProperty initializerproperty))
                        {
                            if (SaveChangeType(initializerproperty.Type, (node.FirstNode as XText).Value.TrimStart('\n', '\r', ' ').TrimEnd('\n', '\r', ' '), out object value))
                            {
                                initializerproperty.SetValue(value);
                            }
                        }
                    }
                }
                else
                {
                    _debug?.Pass(this, "initializer/contentproperty/unexpected", s => string.Format(s, instance));
                }
            }
        }