示例#1
0
        /// <summary>
        /// 向 <see cref="IniConfig"/> 维护的 <see cref="IObject"/> 中设置一个对象并序列化成指定节名的 <see cref="ISection"/>, 若节名已存在将会覆盖整个节
        /// </summary>
        /// <param name="sectionKey">用作要添加的元素的键的对象</param>
        /// <param name="obj">用作要添加的元素的值的对象</param>
        /// <exception cref="ArgumentException">sectionKey 为空字符串或为 <see langword="null"/></exception>
        /// <exception cref="ArgumentNullException">obj 为 null</exception>
        public void SetObject(string sectionKey, object obj)
        {
            if (string.IsNullOrEmpty(sectionKey))
            {
                throw new ArgumentException("sectionKey 不允许为空. 原因: obj 不能设置到为未命名的节点", "sectoinKey");
            }
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            if (!this._object.ContainsKey(sectionKey))                // 如果不存在这个名称
            {
                this._object.Add(new ISection(sectionKey));
            }

            // 获取节对象
            ISection section = this._object[sectionKey];

            if (section.Count > 0)
            {
                section.Clear();
            }

            Type tType = obj.GetType();

            PropertyInfo[] tProperties = tType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (PropertyInfo property in tProperties)
            {
                // 获取属性是否有标记 KeyName
                IniKeyAttribute keyAttribute = property.GetCustomAttribute <IniKeyAttribute> ();

                string key = string.Empty;
                if (keyAttribute != null)
                {
                    key = keyAttribute.KeyName;
                }
                else
                {
                    key = property.Name;
                }

                // 判断 Key 重复
                if (section.ContainsKey(key))
                {
                    throw new ArgumentException("序列化的对象内部存在一个或多个重复的 Key, 这个异常可能由于属性名和特性引起.", "obj");
                }

                // 获取只有0个参数的 Get 方法 (排除索引器)
                MethodInfo method = property.GetGetMethod(true);
                if (method.GetParameters().Count() == 0)
                {
                    section.Add(key, new IValue(method.Invoke(obj, null)));
                }
            }
        }
示例#2
0
        /// <summary>
        /// Resets the specified section.
        /// </summary>
        /// <param name="section"><see cref="ISection" /></param>
        /// <returns><see cref="ICron" /></returns>
        public ICron Reset(ISection section)
        {
            section.Clear();

            return(this);
        }