示例#1
0
 public void RegisterStyle(SelectorKey name, XxElement element)
 {
     styles[name] = element;
 }
        public TInstance CreateObject <TInstance>(XxElement element, bool resolveBindings = true)
        {
            const string TypeNameProperty = "TypeName";
            var          type             = element.Type;
            var          typeDef          = element.Properties.FirstOrDefault(o => o.Key.Name == TypeNameProperty);

            var typeName = typeDef.Value as string;

            if (!string.IsNullOrEmpty(typeName))
            {
                type = Type.GetType(typeName.Replace(';', ',')) ?? type;
            }

            if (!typeof(TInstance).IsAssignableFrom(type))
            {
                throw new InvalidDataException($"Cannot create {typeof(TInstance).Name} from {type.Name}!");
            }
            var instance = objectFactory.Create(type, this, elementTypeMapping);

            if (instance is IStoreElement storeElement)
            {
                storeElement.Element = element;
            }

            var properties = GetProperties(element.Type, element.Properties);

            foreach (var prop in properties)
            {
                if (prop.Value is XxBindingString binding)
                {
                    if (prop.Key.GetCustomAttribute <XxBindingStringAttribute>() != null || !resolveBindings)
                    {
                        prop.Key.SetValue(instance, $"{{{binding.Value}}}");
                    }
                    else
                    {
                        bindingService.AddBinding(instance, prop.Key, binding.Value);
                    }
                }
                else
                {
                    if (prop.Key.PropertyType.IsAssignableFrom(prop.Value.GetType()))
                    {
                        prop.Key.SetValue(instance, prop.Value);
                    }
                    else
                    {
                        var constructor = prop.Key.PropertyType.GetConstructor(new Type[] { prop.Value.GetType() });
                        if (constructor != null)
                        {
                            var obj = constructor.Invoke(new[] { prop.Value });
                            prop.Key.SetValue(instance, obj);
                        }
                    }
                }
            }

            bool childrenAsDefinitions = element.Type.GetCustomAttribute <ChildrenAsDefinitionsAttribute>() != null;

            if (element.Children != null)
            {
                if (instance is IElementsContainer container)
                {
                    var children = new List <object>();

                    foreach (var childElement in element.Children)
                    {
                        if (childrenAsDefinitions)
                        {
                            children.Add(childElement);
                        }
                        else
                        {
                            var child = CreateObject <object>(childElement, !childrenAsDefinitions && resolveBindings);
                            children.Add(child);
                        }
                    }
                    container.InitChildren(children);
                }
                else
                {
                    throw new InvalidOperationException($"Cannot add child elements to {element.Type.Name}");
                }
            }

            return((TInstance)instance);
        }
示例#3
0
 public void InitChildren(IEnumerable <object> elements)
 {
     Element = (XxElement)elements.First();
 }