• 基于Reflect将List泛型数据源转换为Json字符串


    在Web开发中,较为常用的数据传递格式为json键值对字符串格式,而在普遍的ORM全映射或半映射技术中,从后端所获得的数据往往是Enumerable或是List<T>类型的数据,因此做了一个通用类,实现将List<T>类型的数据向json格式数据的转换。代码如下:

    首先建立一个基于Reflect的基础类Reflect,实现对名称为给定字符串的属性的赋值或取值操作:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Collections;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Reflection;
     7 
     8 namespace ReflectionLearning
     9 {
    10     class Reflect
    11     {
    12         private Type _type;
    13         private object _obj;
    14         private PropertyInfo _propertyInfo;
    15 
    16         public Reflect(object obj)
    17         {
    18             this._obj = obj;
    19             _type = obj.GetType();            
    20         }
    21 
    22         /// <summary>
    23         /// 通过反射,传入属性名,获取属性值
    24         /// </summary>
    25         /// <param name="Name"></param>
    26         /// <returns></returns>
    27         public object GetValue(string Name)
    28         {
    29             _propertyInfo = _type.GetProperty(Name);
    30             object result = _propertyInfo.GetValue(_obj, null);
    31             return result;
    32         }
    33 
    34         /// <summary>
    35         /// 传入属性名、属性值,完成属性赋值操作
    36         /// </summary>
    37         /// <param name="Name"></param>
    38         /// <param name="value"></param>
    39         public void SetValue(string Name, object value)
    40         {
    41             _propertyInfo = _type.GetProperty(Name);
    42             _propertyInfo.SetValue(_obj,value, null);
    43         }
    44     }
    45 }

    在通用类中,对Reflect类进行引用,然后通过遍历List<T>中的每个T类的每个属性,进行键值对的转换:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Data;
     5 using System.Text;
     6 
     7 namespace ReflectionLearning
     8 {
     9     class ValueTurning
    10     {
    11         private Reflect reflect;
    12         private string[] parms;
    13         private DataTable dt;
    14         private List<Type> ResultList;
    15 
    16         //以数据源Dt,属性列表Parms,原始列表(其中元素已定义但未实例化)outList
    17         //对此类进行初始化操作
    18         public ValueTurning(DataTable Dt, string[] Parms, List<Type> outList)
    19         {
    20             dt = Dt;
    21             parms = Parms;
    22             ResultList = outList;
    23             beginTurning();
    24             GetJsonFromList(ResultList);
    25         }
    26 
    27         /// <summary>
    28         /// 遍历List中的元素,以Dt中的查询结果对其属性赋值
    29         /// </summary>
    30         public void beginTurning()
    31         {
    32             object obj;
    33             //遍历List中的每一个元素
    34             for (int i = 0; i < dt.Rows.Count; i++)
    35             {
    36                 //访问当前元素,用以初始化reflect反射类
    37                 obj = ResultList[i];
    38                 reflect = new Reflect(obj);
    39 
    40                 //遍历此元素中的每个属性,对其赋值
    41                 for (int j = 0; j < parms.Length; j++)
    42                 {
    43                     //设置属性
    44                     reflect.SetValue(parms[j], dt.Rows[i][parms[j]]);
    45                 }
    46 
    47                 TmpReset(obj);
    48             }
    49         }
    50 
    51         /// <summary>
    52         /// 以ResultList中的元素为数据源,转化为JSON字符串
    53         /// </summary>
    54         /// <returns>Json字符串</returns>
    55         public string GetJsonFromList(List<Type> ResultList)
    56         {
    57             StringBuilder strBuilder = new StringBuilder();
    58             if (ResultList.Count <= 0)
    59             {
    60                 return "";
    61             }
    62             else
    63             {
    64                 int num=ResultList.Count;
    65                 int index = 0;
    66                 object obj;
    67                 for (; index < num; index++)
    68                 {
    69                     obj = ResultList[index];
    70                     reflect = new Reflect(obj);
    71                     string parm = parms[index];
    72                     strBuilder.Append("{");
    73                     strBuilder.Append(couple(parm, reflect.GetValue(parm).ToString()));
    74                     strBuilder.Append("}");
    75                     if (index != num - 1)
    76                         strBuilder.Append(",");
    77                     TmpReset(obj);
    78                 }
    79                 return strBuilder.ToString();
    80             }
    81         }
    82 
    83         //临时对象初始化
    84         private void TmpReset(object obj)
    85         {
    86             obj = null;
    87             reflect = null;
    88         }
    89 
    90         //将键值对转化成 "str1":"str2" 形式
    91         public string couple(string str1, string str2)
    92         {
    93             return (""" + str1 + "":"" + str2 + """);
    94         }
    95     }
    96 }
  • 相关阅读:
    中美贸易战再次开启,世界两极化进程正在加快形成!..... Copyright: 1688澳洲新闻网 Read more at: https://www.1688.com.au/world/international/2018/06/17/369368/
    缠师的博客中关于舒伯特中的回帖,细思极恐
    南怀瑾,脱了国学大师的外衣,只剩下江湖和名利
    你可能修了一个假的“不净观”
    陈大惠老师:什么叫道德?
    C#-正则,常用几种数据解析-端午快乐
    html5+go+websocket简单实例代码
    Task三个列子的分享
    golang-web框架revel一个表单提交的总结
    百度流行音乐-资源数据整合
  • 原文地址:https://www.cnblogs.com/cleverJoe/p/5275348.html
Copyright © 2020-2023  润新知