C#实现实体类与字符串互相转换的方法

所属分类: 软件编程 / C#教程 阅读数: 43
收藏 0 赞 0 分享

本文实例讲述了C#实现实体类与字符串互相转换的方法。分享给大家供大家参考。具体实现方法如下:

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#使用oledb读取excel表格内容到datatable的方法

这篇文章主要介绍了C#使用oledb读取excel表格内容到datatable的方法,涉及C#操作oledb及datatable的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用oledb操作excel文件的方法

这篇文章主要介绍了C#使用oledb操作excel文件的方法,涉及C#中oledb操作excel的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用IHttpModule接口修改http输出的方法

这篇文章主要介绍了C#使用IHttpModule接口修改http输出的方法,涉及C#操作IHttpModule接口的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#给图片加水印的简单实现方法

这篇文章主要介绍了C#给图片加水印的简单实现方法,涉及C#操作图片的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#生成随机数的方法小结

这篇文章主要介绍了C#生成随机数的方法,实例总结了C#生成随机数的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用jQuery实现无刷新评论提交的方法

这篇文章主要介绍了C#使用jQuery实现无刷新评论提交的方法,涉及C#结合jQuery进行Ajax操作的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#读取中文文件出现乱码的解决方法

这篇文章主要介绍了C#读取中文文件出现乱码的解决方法,涉及C#中文编码的操作技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像对比度调整的方法

这篇文章主要介绍了C#图像对比度调整的方法,涉及C#实现图像对比度操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像灰度级拉伸的方法

这篇文章主要介绍了C#图像灰度级拉伸的方法,涉及C#灰度操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像线性变换的方法

这篇文章主要介绍了C#图像线性变换的方法,涉及C#操作图像线性变换的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多