欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

C#實現(xiàn)實體類與字符串互相轉換的方法

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

本文實例講述了C#實現(xiàn)實體類與字符串互相轉換的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

using System;
using System.Collections.Generic;
using System.Text;
namespace PackDLL.Data.ConvertData
{
 /// <summary>
 /// 實體類、字符串互相轉換
 /// </summary>
 public class PackReflectionEntity<T>
 {
  /// <summary>
  /// 將實體類通過反射組裝成字符串
  /// </summary>
  /// <param name="t">實體類</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>
  /// 將反射得到字符串轉換為對象
  /// </summary>
  /// <param name="str">反射得到的字符串</param>
  /// <returns>實體類</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>
  /// 轉換值的類型
  /// </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;
   }
  }
 }
}

希望本文所述對大家的C#程序設計有所幫助。

上一篇:C#實現(xiàn)由四周向中心縮小的窗體退出特效

欄    目:C#教程

下一篇:winform實現(xiàn)創(chuàng)建最前端窗體的方法

本文標題:C#實現(xiàn)實體類與字符串互相轉換的方法

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/7039.html

網頁制作CMS教程網絡編程軟件編程腳本語言數(shù)據庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網 版權所有