예제 #1
0
        private void AddChild(Element parent)
        {
            TypeBrowser browser = TypeBrowser.Create(parent.GetType());
            Element     child   = this.GetElement();

            if (child is UnknownElement)
            {
                PropertyInfo property = browser.FindElement(this.GetXmlComponent());
                if (property != null)
                {
                    AssignValue(parent, property, child.InnerText);

                    // We're not going to add it to the parent, which has the potential to
                    // lose any attributes/child elements assigned to the unknown, but this
                    // is the behaviour of the C++ version.
                    return;
                }
            }
            else if (parent.AddChild(child))
            {
                this.OnElementAdded(child); // Call it here after it's Parent has been set
                return;                     // Not an orphan
            }
            else
            {
                // Lets try an Element as a property?
                this.OnElementAdded(child); // Will be either added as a Property or Orphan

                // Search for a property that we can assign to
                TypeInfo typeInfo = child.GetType().GetTypeInfo();
                foreach (var property in browser.Elements)
                {
                    if (property.Item1.PropertyType.GetTypeInfo().IsAssignableFrom(typeInfo))
                    {
                        property.Item1.SetValue(parent, child, null);
                        return;
                    }
                }
            }

            parent.AddOrphan(child); // Save for later serialization
        }