Ejemplo n.º 1
0
        public static void WriteIni <T>(InIReader reader) where T : class, new()
        {
            Type objType = typeof(T);

            //取属性上的自定义特性
            foreach (FieldInfo fieldInfo in objType.GetFields())
            {
                object[] objAttrs = fieldInfo.GetCustomAttributes(typeof(IniAttribute), true);

                if (objAttrs.Length <= 0)
                {
                    continue;
                }

                IniAttribute attr = objAttrs[0] as IniAttribute;
                if (attr == null)
                {
                    continue;
                }

                if (fieldInfo.FieldType == typeof(bool))
                {
                    reader.Write(attr.Section, fieldInfo.Name, (bool)fieldInfo.GetValue(null));
                }
                else if (fieldInfo.FieldType == typeof(int))
                {
                    reader.Write(attr.Section, fieldInfo.Name, (int)fieldInfo.GetValue(null));
                }
                else if (fieldInfo.FieldType == typeof(string))
                {
                    reader.Write(attr.Section, fieldInfo.Name, (string)fieldInfo.GetValue(null));
                }
            }
        }
Ejemplo n.º 2
0
        public static void ReadIni <T>(InIReader reader) where T : class, new()
        {
            Type objType = typeof(T);

            //取属性上的自定义特性
            foreach (FieldInfo fieldInfo in objType.GetFields())
            {
                object[] objAttrs = fieldInfo.GetCustomAttributes(typeof(IniAttribute), true);
                if (objAttrs.Length <= 0)
                {
                    continue;
                }

                IniAttribute attr = objAttrs[0] as IniAttribute;
                if (attr == null)
                {
                    continue;
                }

                if (Reader.FindValue(attr.Section, fieldInfo.Name) == null)
                {
                    continue;
                }

                if (fieldInfo.FieldType == typeof(bool))
                {
                    bool value = reader.ReadBoolean(attr.Section, fieldInfo.Name, false);
                    fieldInfo.SetValue(null, value);
                }
                else if (fieldInfo.FieldType == typeof(int))
                {
                    int value = reader.ReadInt32(attr.Section, fieldInfo.Name, 0);
                    fieldInfo.SetValue(null, value);
                }
                else if (fieldInfo.FieldType == typeof(string))
                {
                    string value = reader.ReadString(attr.Section, fieldInfo.Name, "");
                    fieldInfo.SetValue(null, value);
                }
            }
        }