示例#1
0
        public XTypedObject CreateInstance(IActivityMonitor monitor, XElement e, IServiceProvider baseProvider = null, Type type = null)
        {
            using (monitor.OpenDebug($"Creating XTypedObject from root {e.ToStringPath()}."))
            {
                if (e == null)
                {
                    throw new ArgumentNullException(nameof(e));
                }
                if (monitor == null)
                {
                    throw new ArgumentNullException(nameof(monitor));
                }
                if (_typeRegister.Count == 0)
                {
                    AutoRegisterFromLoadedAssemblies(monitor);
                }

                e.Changing += PreventAnyChangesToXElement;
                var eReader = new XElementReader(monitor, e, new HashSet <XObject>());

                if (type == null)
                {
                    type = GetMappping(eReader);
                }
                var rootConfig = new XTypedObject.Initializer(this, eReader, baseProvider);
                var root       = (XTypedObject)baseProvider.SimpleObjectCreate(monitor, type, rootConfig);
                var result     = root != null && CreateChildren(root, rootConfig) ? root : null;
                if (result != null)
                {
                    eReader.WarnUnhandledAttributes();
                }
                return(result);
            }
        }
示例#2
0
        static bool CreateChildren(XTypedObject parent, XTypedObject.Initializer parentConfig)
        {
            SimpleServiceContainer cChild      = null;
            List <XTypedObject>    created     = null;
            XTypedFactory          typeFactory = null;
            var eParent = parent.XElement;

            foreach (var child in eParent.Elements())
            {
                if (typeFactory == null)
                {
                    typeFactory = parentConfig.ChildServices.GetService <XTypedFactory>(true);
                }
                var rChild = parentConfig.Reader.WithElement(child, false);
                var tChild = typeFactory.GetMappping(rChild);
                if (tChild != null)
                {
                    if (cChild == null)
                    {
                        cChild = new SimpleServiceContainer(parentConfig.ChildServices);
                    }
                    var config = new XTypedObject.Initializer(parent, rChild, cChild);
                    var o      = (XTypedObject)cChild.SimpleObjectCreate(rChild.Monitor, tChild, config);
                    if (created == null)
                    {
                        created = new List <XTypedObject>();
                    }
                    created.Add(o);
                    if (o == null || !CreateChildren(o, config))
                    {
                        return(false);
                    }
                    rChild.WarnUnhandledAttributes();
                }
            }
            return(parent.OnChildrenCreated(parentConfig, (IReadOnlyList <XTypedObject>)created ?? Array.Empty <XTypedObject>()));
        }