C#类对象数据存储(Object⾃定义序列化)获取Object对象的所有成员变量:
FieldInfo[] fields = obj.GetType().GetFields();    // obj可以为任意类型对象
获取变量名称:
string name = field.Name;
获取变量值:
Object valueObj = field.GetValue(obj);
设置obj中指定变量的值:
string value = "123";
Type type = field.FieldType;
object valueObj = Convert.ChangeType(value, type);  // 转化为变量对应类型
field.SetValue(obj, valueObj);                      // 设置值
类对象数据存储⽰例:
源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace SciTools
{
public class OrderParam
{
public string orderId = "";
public string param = "";
public int count = 0;
#region 类对象逻辑
public OrderParam(string orderId = "", string param = "", int count = 0)        {
this.param = param;
}
/// <summary>
/// 从data数据构建OrderParam
/// </summary>
public static OrderParam Parse(string data)
{
OrderParam I = new OrderParam();
StringTool.setAllFields(I, data);
return I;
}
//{
//    #orderId#(0005)#orderId#
//    #param#(参数)#param#
//    #count#(12)#count#
//}
/
// <summary>
/// 将所有成员变量转化为字符串格式
/// </summary>
public string ToString()
{
AllFields(this);
}
#endregion
#region List数组与String互转
public static string ToString(List<OrderParam> list)
{
StringBuilder sb = new StringBuilder();
foreach(OrderParam param in list)
{
sb.AppendLine(param.ToString());
}
return sb.ToString();
}
public static List<OrderParam> ParseList(string data)
{
List<OrderParam> list = new List<OrderParam>();
if (!data.Trim().Equals(""))
{
foreach (string iteam0 in data.Split('}'))
{
string iteam = iteam0.Trim();
if (iteam.Equals("")) continue;
if (iteam.StartsWith("{")) iteam = iteam.Substring(1).Trim();
OrderParam param = OrderParam.Parse(iteam);
list.Add(param);
}
}
return list;
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace SciTools
{
/
// <summary>
/// 读取Object对象的所有成员变量, 将类中所有变量信息转化为字符串进⾏存储
/// </summary>
public class StringTool
{
#region 成员变量序列化(转化为string、从string赋值)
/// <summary>
/// 获取Object对象,所有成员变量信息
/// </summary>
public static string getAllFields(Object obj)
{
string data = "\r\n";
FieldInfo[] fields = obj.GetType().GetFields();
foreach (FieldInfo field in fields)
{
string name = field.Name;                      // 类变量名称
string value = field.GetValue(obj).ToString();  // 类变量值
data += "\t" + NodeStr(value, name, false) + "\r\n";
}
data = "{" + data + "}";
return data;
}
/
// <summary>
/// 设置Object对象的所有成员变量值(从data中解析数据)
/// </summary>
/// <param name="obj"></param>
/// <param name="data"></param>
public static void setAllFields(Object obj, string data)
{
FieldInfo[] fields = obj.GetType().GetFields();
foreach (FieldInfo field in fields)
{
string name = field.Name;
string value = getNodeData(data, name, false);      // 获取变量字符串值
Type type = field.FieldType;
object valueObj = Convert.ChangeType(value, type);  // 转化为变量对应类型
field.SetValue(obj, valueObj);                      // 设置值
}
}
#endregion
#region 节点数据⽣成、读取
/// <summary>
/// 从⾃定义格式的数据data中,获取nodeName对应的节点数据
///
/
param name// NeedToRegister(false)NeedToRegister    // finalNode = false;
/// RegisterPrice(1)                        // finalNode = true;
/// </summary>
/// <param name="data">待解析的数据</param>
/// <param name="nodeName">节点名称</param>
/// <param name="finalNode">是否为叶节点</param>
/// <returns></returns>
public static string getNodeData(string data, string nodeName, bool finalNode)        {
nodeName = "#" + nodeName + "#";
if (!data.Contains(nodeName)) return "";
try
{
string S = nodeName + "(", E = ")" + (finalNode ? "" : nodeName);
int indexS = data.IndexOf(S) + S.Length;
int indexE = data.IndexOf(E, indexS);
return data.Substring(indexS, indexE - indexS);
}
catch (Exception) { return data; }
}
/// <summary>
/// 将数据转化为节点形式
/// </summary>
public static string NodeStr(string data, string nodeName, bool finalNode)
{
nodeName = "#" + nodeName + "#";
if (!finalNode) return nodeName + "(" + data + ")" + nodeName;
else return nodeName + "(" + data + ")";
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace SciTools
{
/// <summary>
/// 数据存储
/// </summary>
public class FileDB
{
static FileDB instance = null;
public static FileDB Instance()
{
if (instance == null) instance = new FileDB(AppDomain.CurrentDomain.BaseDirectory + "DB.txt");            return instance;
}
//----------------------
string filePath = "";                  // ⽂件保存路径
public List<OrderParam> list = null;    // 数据信息
FileDB(string filePath)
{
this.filePath = filePath;
string fileData = FileProcess.fileToString(filePath);
list = OrderParam.ParseList(fileData);
}
/// <summary>
/// 保存数据
/// </summary>
public void Save()
{
string data = OrderParam.ToString(list);
FileProcess.SaveProcess(data, filePath);
}
/// <summary>
/
// 清空数据
/// </summary>
public void Clear()
{
list.Clear();
FileProcess.SaveProcess("", filePath);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SciTools
{
public class FileProcess
{

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。