Beispiel #1
0
        private Builtin ResolveBuiltin(XmlReader reader, Builtin parent)
        {
            if (reader == null || reader.NodeType != XmlNodeType.Element)
            {
                return(null);
            }

            Builtin builtin = parent.Plugin.CreateBuiltin(reader.Name, reader.GetAttribute("name"));

            //设置当前构件的其他属性并将构件对象挂载到插件树中,该方法不会引起读取器的位置或状态变化。
            this.UpdateBuiltin(parent.FullPath, builtin, reader);

            //解析当前构件的内部元素(包括下属的子构件或扩展元素)
            this.ResolveBuiltinContent(reader, builtin);

            return(builtin);
        }
        public BuiltinBehavior(Builtin builtin, string name, string text = null)
        {
            if (builtin == null)
            {
                throw new ArgumentNullException("builtin");
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("name");
            }

            _builtin    = builtin;
            _name       = name.Trim();
            _text       = text;
            _properties = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
        }
        internal static void UpdateProperties(object target, Builtin builtin, IEnumerable <string> ignoredProperties)
        {
            if (target == null || builtin == null)
            {
                return;
            }

            foreach (string propertyName in builtin.Properties.AllKeys)
            {
                //如果当前属性名为忽略属性则忽略设置
                if (ignoredProperties != null && ignoredProperties.Contains(propertyName, StringComparer.OrdinalIgnoreCase))
                {
                    continue;
                }

                try
                {
                    var propertyType = Tiandao.Common.Converter.GetMemberType(target, propertyName);
                    Tiandao.Common.Converter.SetValue(target, propertyName, builtin.Properties.GetValue(propertyName, propertyType, null));
                }
                catch (Exception ex)
                {
                    StringBuilder message = new StringBuilder();

                    message.AppendFormat("{0}[{1}]", ex.Message, ex.Source);
                    message.AppendLine();

                    if (ex.InnerException != null)
                    {
                        message.AppendFormat("\t{0}: {1}[{2}]", ex.GetType().FullName, ex.Message, ex.Source);
                        message.AppendLine();
                    }

                    message.AppendFormat("\tOccurred an exception on set '{1}' property of '{0}' builtin, it's raw value is \"{2}\", The target type of builtin is '{3}'.",
                                         builtin.ToString(),
                                         propertyName,
                                         builtin.Properties[propertyName].RawValue,
                                         target.GetType().AssemblyQualifiedName);

                    //输出错误日志
                    Tiandao.Diagnostics.Logger.Error(null, message.ToString(), ex);

                    throw new PluginException(FailureCodes.BuiltinBuildFailed, message.ToString(), ex);
                }
            }
        }
        public static object BuildBuiltin(Builtin builtin, IEnumerable <string> ignoredProperties = null)
        {
            if (builtin == null)
            {
                throw new ArgumentNullException("builtin");
            }

            var result = PluginUtility.BuildType(builtin);

            //设置更新目标对象的属性集
            if (result != null)
            {
                UpdateProperties(result, builtin, ignoredProperties);
            }

            return(result);
        }
Beispiel #5
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            Builtin target = obj as Builtin;

            if (target == null)
            {
                return(false);
            }

            return(string.Equals(_builderName, target.BuilderName, StringComparison.OrdinalIgnoreCase) &&
                   string.Equals(this.FullPath, target.FullPath, StringComparison.OrdinalIgnoreCase));
        }
Beispiel #6
0
        public BuiltinType(Builtin builtin, string typeName)
        {
            if (builtin == null)
            {
                throw new ArgumentNullException("builtin");
            }

            if (string.IsNullOrWhiteSpace(typeName))
            {
                throw new ArgumentNullException("typeName");
            }

            _type        = null;
            _builtin     = builtin;
            _typeName    = typeName.Trim();
            _constructor = new BuiltinTypeConstructor(this);
        }
Beispiel #7
0
        private void UnmountItem(Plugin plugin, PluginTreeNode node)
        {
            if (node == null)
            {
                return;
            }

            if (plugin != null)
            {
                foreach (PluginTreeNode child in node.Children)
                {
                    this.UnmountItem(plugin, child);
                }
            }

            if (node.NodeType == PluginTreeNodeType.Custom)
            {
                this.Unmount(node);
                return;
            }

            if (node.NodeType == PluginTreeNodeType.Builtin)
            {
                Builtin builtin = (Builtin)node.Value;

                if (string.ReferenceEquals(builtin.Plugin, plugin))
                {
                    IBuilder builder = node.Plugin.GetBuilder(builtin.BuilderName);
                    if (builder != null)
                    {
                        builder.Destroy(Builders.BuilderContext.CreateContext(builder, builtin, this));
                    }

                    plugin.UnregisterBuiltin(builtin);
                    node.Value = null;
                }
            }

            if (node.Children.Count < 1 && node.Parent != null)
            {
                node.Parent.Children.Remove(node);
            }
        }
Beispiel #8
0
        private void UpdateBuiltin(string path, Builtin builtin, XmlReader reader)
        {
            //循环读取当前构件元素的所有特性,并设置到构件对象的扩展属性集合中
            for (int i = 0; i < reader.AttributeCount; i++)
            {
                reader.MoveToAttribute(i);

                switch (reader.Name.ToLowerInvariant())
                {
                case "name":
                    continue;

                case "position":
                    builtin.Position = reader.Value;
                    break;

                case "type":
                    if (builtin.BuiltinType == null)
                    {
                        builtin.BuiltinType = new BuiltinType(builtin, reader.Value);
                    }
                    else
                    {
                        builtin.BuiltinType.TypeName = reader.Value;
                    }
                    break;

                default:
                    builtin.Properties.Set(reader.Name, reader.Value);
                    break;
                }
            }

            //将当前读取器的位置重新移回该元素节点上
            if (reader.NodeType == XmlNodeType.Attribute)
            {
                reader.MoveToElement();
            }

            //在插件树中挂载当前构件对象
            this.PluginTree.MountBuiltin(path, builtin);
        }
Beispiel #9
0
        private void ResolveExtendedElement(XmlReader reader, Builtin builtin)
        {
            if (builtin == null)
            {
                throw new ArgumentNullException("builtin");
            }

            var parts = reader.Name.Split('.');

            if (parts.Length != 2)
            {
                throw new PluginException(string.Format("Invalid '{0}' ExtendElement in '{1}'.", reader.Name, builtin.ToString()));
            }

            if (string.Equals(parts[0], builtin.BuilderName, StringComparison.OrdinalIgnoreCase))
            {
                switch (parts[1].ToLowerInvariant())
                {
                case "constructor":
                    if (builtin.BuiltinType == null)
                    {
                        throw new PluginException(string.Format("This '{0}' ExtendElement dependencied builtin-type is null.", reader.Name));
                    }

                    this.ResolveBuiltinConstructor(reader, builtin.BuiltinType.Constructor);
                    break;

                default:
                    this.ResolveBuiltinBehavior(reader, builtin, parts[1]);
                    break;
                }
            }
            else if (string.Equals(parts[0], builtin.Name, StringComparison.OrdinalIgnoreCase))
            {
                string propertyName = string.Join(".", parts, 1, parts.Length - 1);
                builtin.Properties.Set(propertyName, this.ResolveExtendedProperty(reader, builtin.Plugin, builtin.FullPath));
            }
            else
            {
                throw new PluginException(string.Format("Invalid '{0}' ExtendElement in '{1}'.", reader.Name, builtin.ToString()));
            }
        }
        private void SetBuiltin(Builtin builtin)
        {
            if (builtin == null)
            {
                throw new ArgumentNullException("builtin");
            }

            if (builtin.Node != null)
            {
                throw new InvalidOperationException();
            }

            //将构建的所属节点指向本节点
            builtin.Node = this;

            //更新当前节点的属性值
            _value      = builtin;
            _nodeType   = PluginTreeNodeType.Builtin;
            this.Plugin = builtin.Plugin;
        }
Beispiel #11
0
        internal static object BuildType(Builtin builtin)
        {
            if (builtin == null)
            {
                throw new ArgumentNullException("builtin");
            }

            if (builtin.BuiltinType != null)
            {
                return(BuildType(builtin.BuiltinType));
            }

            string typeName = builtin.Properties.GetValue <string>("type");

            if (string.IsNullOrWhiteSpace(typeName))
            {
                return(null);
            }
            else
            {
                return(BuildType(typeName, builtin));
            }
        }
Beispiel #12
0
        internal void MountBuiltin(string path, Builtin builtin)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException("path");
            }

            if (builtin == null)
            {
                throw new ArgumentNullException("builtin");
            }

            var fullPath = PluginPath.Combine(path, builtin.Name);

            //激发“Mounting”事件
            this.OnMounting(new PluginMountEventArgs(fullPath, builtin));

            //创建对应的构件节点
            this.MountItem(fullPath, builtin, builtin.Position);

            //激发“Mounted”事件
            this.OnMounted(new PluginMountEventArgs(fullPath, builtin));
        }
Beispiel #13
0
        private void ResolveBuiltinContent(XmlReader reader, Builtin builtin)
        {
            if (reader.IsEmptyElement)
            {
                return;
            }

            int depth = reader.Depth;

            while (reader.Read() && reader.Depth > depth)
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (this.IsExtendElement(reader.Name))
                    {
                        this.ResolveExtendedElement(reader, builtin);
                    }
                    else
                    {
                        this.ResolveBuiltin(reader, builtin);
                    }
                }
            }
        }
Beispiel #14
0
        private static void InjectProperties(object target, Builtin builtin)
        {
            if (target == null || builtin == null)
            {
                return;
            }

            //获取当前构件所属的服务容器
            var serviceProvider = FindServiceProvider(builtin);

            if (serviceProvider == null)
            {
                return;
            }

#if !CORE_CLR
            //查找指定目标对象需要注入的属性和字段集(支持对非公共成员的注入)
            var members = target.GetType().FindMembers(MemberTypes.Field | MemberTypes.Property, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, (m, t) => m.GetCustomAttribute((Type)t, true) != null, typeof(Tiandao.Services.ServiceDependencyAttribute)).ToList();
#else
            var members = target.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(member => member.GetCustomAttribute(typeof(Tiandao.Services.ServiceDependencyAttribute), true) != null).ToList();
#endif

            if (members == null || members.Count < 1)
            {
                return;
            }

            object memberValue;

            foreach (var member in members)
            {
                //如果当前成员已经在构件属性集中显式存在则跳过
                if (builtin.HasProperties && builtin.Properties.Contains(member.Name))
                {
                    continue;
                }

                //获取需注入成员的注入标记
                var attribute = (Tiandao.Services.ServiceDependencyAttribute)member.GetCustomAttribute(typeof(Tiandao.Services.ServiceDependencyAttribute), true);

                if (member.IsField())
                {
                    if (!string.IsNullOrWhiteSpace(attribute.Name))
                    {
                        memberValue = serviceProvider.Resolve(attribute.Name);
                    }
                    else
                    {
                        memberValue = serviceProvider.Resolve(attribute.Contract ?? ((FieldInfo)member).FieldType);
                    }

                    ((FieldInfo)member).SetValue(target, memberValue);
                }
                else if (member.IsProperty())
                {
                    if (!string.IsNullOrWhiteSpace(attribute.Name))
                    {
                        memberValue = serviceProvider.Resolve(attribute.Name);
                    }
                    else
                    {
                        memberValue = serviceProvider.Resolve(attribute.Contract ?? ((PropertyInfo)member).PropertyType);
                    }

                    ((PropertyInfo)member).SetValue(target, memberValue);
                }
            }
        }