Beispiel #1
0
        private SectionBinding FindBindingBySectionName(PropertyInfo[] properties, string name)
        {
            foreach (PropertyInfo prop in properties)
            {
                IniSectionAttribute attr = prop.GetCustomAttribute <IniSectionAttribute>();
                if (attr != null && attr.Name == name)
                {
                    return(new SectionBinding(attr.Name, prop));
                }
            }

            return(properties
                   .Where(prop => prop.Name == name)
                   .Select(prop => new SectionBinding(prop.Name, prop))
                   .FirstOrDefault());
        }
Beispiel #2
0
        /// <summary>
        /// 从 <see cref="IniConfig"/> 维护的 <see cref="IObject"/> 中获取一个 <see cref="ISection"/> 反序列化成指定类型的对象.
        /// </summary>
        /// <param name="t">用作要转换目标对象的类型的对象, 同时获取的节名称由这个类型的名称或由标记的 <see cref="IniSectionAttribute"/> 特性决定</param>
        /// <exception cref="ArgumentNullException">t 为 null</exception>
        /// <exception cref="ArgumentException">t 不是 <see cref="RuntimeType"/>。 或 t 是开放式泛型类型(即,<see cref="Type.ContainsGenericParameters"/> 属性将返回 <see langword="true"/>)</exception>
        /// <exception cref="NotSupportedException">t 不能为 <see cref="TypeBuilder"/>。 或 不支持创建 <see cref="TypedReference"/>、<see cref="ArgIterator"/>、<see cref="Void"/>和 <see cref="RuntimeArgumentHandle"/> 类型,或者这些类型的数组。 或 包含 t 的程序集是一个用 <see cref="AssemblyBuilderAccess.Save"/> 创建的动态程序集</exception>
        /// <exception cref="TargetInvocationException">正在被调用的构造函数引发了一个异常</exception>
        /// <exception cref="MethodAccessException">调用方没有权限调用此构造函数</exception>
        /// <exception cref="MemberAccessException">无法创建抽象类的实例,或者此成员是使用晚期绑定机制调用的</exception>
        /// <exception cref="InvalidComObjectException">未通过 Overload:<see cref="Type.GetTypeFromProgID"/> 或 Overload:<see cref="Type.GetTypeFromCLSID"/> 获取 COM 类型</exception>
        /// <exception cref="COMException">t 是一个 COM 对象,但用于获取类型的类标识符无效,或标识的类未注册</exception>
        /// <exception cref="TypeLoadException">t 不是有效类型</exception>
        /// <exception cref="TargetException">尝试调用一个不存在的属性</exception>
        /// <returns>指定的对象实例</returns>
        public object GetObject(Type t)
        {
            if (t == null)
            {
                throw new ArgumentNullException("t");
            }

            string key = string.Empty;
            IniSectionAttribute sectionAttribute = t.GetCustomAttribute <IniSectionAttribute> ();

            if (sectionAttribute != null)
            {
                key = sectionAttribute.SectionName;
            }
            else
            {
                key = t.Name;
            }

            return(this.GetObject(key, t));
        }
Beispiel #3
0
        /// <summary>
        /// 向 <see cref="IniConfig"/> 维护的 <see cref="IObject"/> 中设置一个对象并序列化成 <see cref="ISection"/>, 若存在重复节名的对象将会覆盖整个节
        /// </summary>
        /// <param name="obj">用作要添加的元素的值的对象</param>
        /// <exception cref="ArgumentNullException">obj 为 null</exception>
        public void SetObject(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            Type tType = obj.GetType();

            // 获取节点名称
            string key = string.Empty;
            IniSectionAttribute sectionAttribute = tType.GetCustomAttribute <IniSectionAttribute> ();

            if (sectionAttribute != null)
            {
                key = sectionAttribute.SectionName;
            }
            else
            {
                key = tType.Name;
            }

            this.SetObject(key, obj);
        }