예제 #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
 private static void TestTimeSection(ICron cron, ISection section)
 {
     section.Add(1)
     .Every.Should()
     .BeFalse();
     cron.Add(section.Time, 2);
     section.Every.Should()
     .BeFalse();
     cron.Add(section.Time, 3, true);
     section.Every.Should()
     .BeTrue();
 }
예제 #3
0
        private static void TestDateSection(ICron cron, ISection section)
        {
            section.Add(1)
            .Every.Should()
            .BeFalse();
            cron.Add(section.Time, 2);
            section.Every.Should()
            .BeFalse();
            Action act = () => cron.Add(section.Time, 3, true);

            act.Should()
            .Throw <Exception>();
        }
예제 #4
0
 private void FillElements(ISection section, IEnumerable <ElementDescription> elementDescriptions)
 {
     if (elementDescriptions != null)
     {
         foreach (var elementDescription in elementDescriptions)
         {
             var element = Build(elementDescription);
             if (element != null)
             {
                 section.Add(element);
             }
         }
     }
 }
예제 #5
0
 private void FillElements(ISection section, IEnumerable<ElementDescription> elementDescriptions)
 {
     if (elementDescriptions != null)
     {
         foreach (var elementDescription in elementDescriptions)
         {
             var element = Build(elementDescription);
             if (element != null)
                 section.Add(element);
             }
     }
 }