C#實(shí)現(xiàn)實(shí)體類與字符串互相轉(zhuǎn)換的方法
本文實(shí)例講述了C#實(shí)現(xiàn)實(shí)體類與字符串互相轉(zhuǎn)換的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
using System; using System.Collections.Generic; using System.Text; namespace PackDLL.Data.ConvertData { /// <summary> /// 實(shí)體類、字符串互相轉(zhuǎn)換 /// </summary> public class PackReflectionEntity<T> { /// <summary> /// 將實(shí)體類通過反射組裝成字符串 /// </summary> /// <param name="t">實(shí)體類</param> /// <returns>組裝的字符串</returns> public static string GetEntityToString(T t) { System.Text.StringBuilder sb = new StringBuilder(); Type type = t.GetType(); System.Reflection.PropertyInfo[] propertyInfos = type.GetProperties(); for (int i = 0; i < propertyInfos.Length; i++) { sb.Append(propertyInfos[i].Name + ":" + propertyInfos[i].GetValue(t, null) + ","); } return sb.ToString().TrimEnd(new char[] { ',' }); } /// <summary> /// 將反射得到字符串轉(zhuǎn)換為對(duì)象 /// </summary> /// <param name="str">反射得到的字符串</param> /// <returns>實(shí)體類</returns> public static T GetEntityStringToEntity(string str) { string[] array = str.Split(','); string[] temp = null; Dictionary<string, string> dictionary = new Dictionary<string, string>(); foreach (string s in array) { temp = s.Split(':'); dictionary.Add(temp[0], temp[1]); } System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof(T)); T entry = (T)assembly.CreateInstance(typeof(T).FullName); System.Text.StringBuilder sb = new StringBuilder(); Type type = entry.GetType(); System.Reflection.PropertyInfo[] propertyInfos = type.GetProperties(); for (int i = 0; i < propertyInfos.Length; i++) { foreach (string key in dictionary.Keys) { if (propertyInfos[i].Name == key.ToString()) { propertyInfos[i].SetValue(entry, GetObject(propertyInfos[i], dictionary[key]), null); break; } } } return entry; } /// <summary> /// 轉(zhuǎn)換值的類型 /// </summary> /// <param name="p"></param> /// <param name="value"></param> /// <returns></returns> static object GetObject(System.Reflection.PropertyInfo p, string value) { switch (p.PropertyType.Name.ToString().ToLower()) { case "int16": return Convert.ToInt16(value); case "int32": return Convert.ToInt32(value); case "int64": return Convert.ToInt64(value); case "string": return Convert.ToString(value); case "datetime": return Convert.ToDateTime(value); case "boolean": return Convert.ToBoolean(value); case "char": return Convert.ToChar(value); case "double": return Convert.ToDouble(value); default: return value; } } } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#和JavaScript實(shí)現(xiàn)交互的方法
最近做一個(gè)小項(xiàng)目不可避免的需要前端腳本與后臺(tái)進(jìn)行交互。由于是在asp.net中實(shí)現(xiàn),故問題演化成asp.net中jiavascript與后臺(tái)c#如何進(jìn)行交互。2015-05-05C#使用semaphore來管理異步下載請(qǐng)求的方法
這篇文章主要介紹了C#使用semaphore來管理異步下載請(qǐng)求的方法,涉及C#使用semaphore實(shí)現(xiàn)多線程管理的技巧,需要的朋友可以參考下2015-06-06C#中括號(hào)強(qiáng)轉(zhuǎn)、as、is區(qū)別詳解
本文主要介紹了C#中括號(hào)強(qiáng)轉(zhuǎn)、as、is區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02c#使用process.start啟動(dòng)程序報(bào)錯(cuò)解決方法
c#使用process.start啟動(dòng)程序報(bào)錯(cuò)解決方法,大家參考使用吧2013-12-12c# 實(shí)現(xiàn)文件上傳下載功能的實(shí)例代碼
這篇文章主要介紹了如何用c# 實(shí)現(xiàn)文件上傳下載功能,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07SQLite之C#版 System.Data.SQLite使用方法
這篇文章主要介紹了SQLite之C#版 System.Data.SQLite使用方法,需要的朋友可以參考下2020-10-10Unity中的PostProcessScene實(shí)用案例深入解析
這篇文章主要為大家介紹了Unity中的PostProcessScene實(shí)用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05