首页 新闻 会员 周边

通过反射将ini文件像配置文件xml一样序列化将属性的值写入ini文件,怎样写入iniini文件

0
悬赏园豆:60 [待解决问题]

我要通过反射给属性赋值,就是把属性的值写入到ini文件 config.comset.PCCom = 5;就是我要获取到config的类型,属性是每个key值,节点是key的所属于的那个comset类

形式:

 CIniConfig config

 config.comset.PCCom = 5;

           config.Save();--写入到ini文件

WriteValue(string path, string Section, string key, string value)--写入ini文件的方法

//

 

--读取ini文件

//定义的ini文件的结构类

    public class CIniConfig : IniConfig   {      public comset comset = new comset();       }   public class comset   {

      public int PCCom       {           get;           set;       }

      public int PCBot       {           get;           set;       }

      public int TempCom       {           get;           set;       }

      public int TempBot       {           get;           set;       }   } }

//获取ini文件的值

 public static T Load<T>(string path) where T : new()
        {
            T temp = new T();
            foreach (FieldInfo field in typeof(T).GetFields())
            {
                object obj = field.GetValue(temp);
                foreach (PropertyInfo prorerty in field.FieldType.GetProperties())
                {
                    prorerty.SetValue(obj, Convert.ChangeType(ContentValue(path, field.Name, prorerty.Name), prorerty.PropertyType), null);
                }
            }
            return temp;
        }
        CIniConfig config = CIniConfig.Load<CIniConfig>("H:\\ini文件读取\\WindowsFormsApplication1\\bin\\Debug\\WIM_SHKJ.ini");

config.comset.PCCom--获取到了ini文件的值

wwwzhang的主页 wwwzhang | 初学一级 | 园豆:37
提问于:2015-08-13 21:09
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册