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

24 篇文章 0 订阅

这篇文章主要介绍了C#实现实体类与字符串互相转换的方法,涉及C#字符串及对象的相互转换技巧,具有一定参考借鉴价值,需要的朋友可以参考下

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

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#程序设计有所帮助。

转自:微点阅读   https://www.weidianyuedu.com

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值