前言:最近學習C#高級課程,里面學到了利用反射和可以得到屬性的特性、屬性名、屬性值,還有拓展方法,一直想將學到的東西利用起來,剛好今天在研究PropertyGrid控件時,想方便一點保存屬性值到配置文件,當然同理也可以保存對象的屬性值到Xml文件 ,類似Json文件有JSON序列化保存更快,但是其中用到了反射技術,在屬性多的情況下,性能會低一點. 后面有時間添加讀取反序列化的.
IniHelper類代碼如下,提供了讀寫方法接口
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;namespace Files
{public class IniHelper{[DllImport("kernel32")]private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);[DllImport("kernel32")]private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);/// <summary>/// 泛型讀取/// </summary>/// <typeparam name="T">值類型</typeparam>/// <param name="section">部分</param>/// <param name="key">鍵名</param>/// <param name="filePath">路徑</param>/// <param name="Default">默認值</param>/// <returns></returns>public static T Read<T>(string section, string key, string filePath,T Default = default(T)){StringBuilder temp = new StringBuilder(255);GetPrivateProfileString(section, key,"", temp, 255, filePath);try{string[] str = temp.ToString().Split('%');if(str[0] != ""){return (T)(TypeDescriptor.GetConverter(typeof(T))).ConvertFromInvariantString(str[0]);}else{return (T)Default;}}catch (Exception err){return (T)Default;}}/// <summary>/// 普通讀取/// </summary>/// <param name="section">部分</param>/// <param name="key">鍵名</param>/// <param name="filePath">路徑</param>/// <param name="Default">默認值</param>/// <returns></returns>public static string Read(string section, string key, string filePath,string Default){StringBuilder temp = new StringBuilder(255);GetPrivateProfileString(section, key, "", temp, 255, filePath);string[] s = temp.ToString().Split(new char[] { '%' });if (s[0] != ""){return s[0].ToString();}else{return Default;}}/// <summary>/// 寫入值/// </summary>/// <param name="section">部分</param>/// <param name="key">鍵名</param>/// <param name="value">值</param>/// <param name="filePath">路徑</param>public static void Write(string section, string key, string value, string filePath){if(WritePrivateProfileString(section, key, value, filePath) <= 0){WritePrivateProfileString(section, key, value, filePath);}}/// <summary>/// Ini保存類屬性分組,屬性名,屬性值到Ini文件/// </summary>/// <typeparam name="T">泛型對象</typeparam>/// <param name="t">具體泛型對象</param>/// <param name="filePath">文件完整路徑</param>public static void SaveObj<T>(T t, string filePath,string DefaultSection = "Default") where T:class{Type type = t.GetType();foreach (var item in type.GetProperties()){string Section = item.PropertyGetCategoryAttribute() ?? DefaultSection;string Key = item.Name.ToString();string Value = item.GetValue(t).ToString();Write(Section, Key, Value, filePath);}}}
}
下面是拓展方法的代碼如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;/// <summary>/// 屬性拓展類/// </summary>public static class PropertyExpand{/// <summary>/// 通過屬性 返回 其特性分組名/// </summary>/// <param name="propertyInfo">屬性類型約束</param>/// <returns></returns>public static string PropertyGetCategoryAttribute(this PropertyInfo propertyInfo){var section = propertyInfo.GetCustomAttributes(typeof(CategoryAttribute), false);return section.Length > 0 ? ((CategoryAttribute)section[0]).Category : null;}}
相當于一個實操筆記,共勉!!!