You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
6.5 KiB
C#
158 lines
6.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Web.Script.Serialization;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Data;
|
|
using System.Collections;
|
|
|
|
namespace NetLibrary
|
|
{
|
|
public static class ListExtension
|
|
{
|
|
#region 转JSON文件
|
|
/// <summary>
|
|
/// 转JSON文件
|
|
/// </summary>
|
|
/// <param name="obj">扩展对象</param>
|
|
/// <param name="filePath">文件路径</param>
|
|
public static void ToJSON(this Object obj, string filePath)
|
|
{
|
|
string jsonString = new JavaScriptSerializer().Serialize(obj);
|
|
string DirectoryName = Path.GetDirectoryName(filePath);
|
|
if (System.IO.Directory.Exists(DirectoryName) == false) System.IO.Directory.CreateDirectory(DirectoryName);
|
|
string fileName = Path.GetFileName(filePath);
|
|
File.WriteAllText(filePath, jsonString);
|
|
}
|
|
#endregion
|
|
#region List转DataTable
|
|
/// <summary>
|
|
/// List转DataTable
|
|
/// </summary>
|
|
/// <typeparam name="TResult">对象</typeparam>
|
|
/// <param name="ListModel">扩展对象</param>
|
|
/// <returns></returns>
|
|
public static DataTable ToDataTable<TResult>(this IEnumerable<TResult> ListModel) where TResult : class
|
|
{
|
|
//创建属性的集合
|
|
List<PropertyInfo> pList = new List<PropertyInfo>();
|
|
//获得反射的入口
|
|
Type type = typeof(TResult);
|
|
DataTable dt = new DataTable();
|
|
//把所有的public属性加入到集合 并添加DataTable的列
|
|
Array.ForEach<PropertyInfo>(type.GetProperties(), item =>
|
|
{
|
|
pList.Add(item);
|
|
Type columnType = item.PropertyType;
|
|
if (item.PropertyType.IsGenericType && item.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
|
|
{
|
|
columnType = item.PropertyType.GetGenericArguments()[0];
|
|
|
|
}
|
|
dt.Columns.Add(new DataColumn(item.Name, columnType));
|
|
});
|
|
foreach (var model in ListModel)
|
|
{
|
|
//创建一个DataRow实例
|
|
DataRow row = dt.NewRow();
|
|
//给row 赋值
|
|
pList.ForEach(item =>
|
|
{
|
|
object value = item.GetValue(model, null);
|
|
if (value != null) row[item.Name] = value;
|
|
});
|
|
//加入到DataTable
|
|
dt.Rows.Add(row);
|
|
}
|
|
return dt;
|
|
}
|
|
#endregion
|
|
#region List序列化Json
|
|
public static string ToJson<TResult>(this IEnumerable<TResult> list, List<string> ListSerializePropertieName) where TResult : class
|
|
{
|
|
//创建属性的集合
|
|
List<PropertyInfo> pList = new List<PropertyInfo>();
|
|
//获得反射的入口
|
|
Type type = typeof(TResult);
|
|
DataTable dt = new DataTable();
|
|
//把所有的public属性加入到集合 并添加DataTable的列
|
|
Array.ForEach<PropertyInfo>(type.GetProperties(), item =>
|
|
{
|
|
pList.Add(item);
|
|
Type columnType = item.PropertyType;
|
|
if (item.PropertyType.IsGenericType && item.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
|
|
{
|
|
columnType = item.PropertyType.GetGenericArguments()[0];
|
|
|
|
}
|
|
dt.Columns.Add(new DataColumn(item.Name, columnType));
|
|
});
|
|
DateTime time = new DateTime(0x7b2, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
JavaScriptSerializer parseJSON = new JavaScriptSerializer();
|
|
long DatetimeMinTimeTicks = time.Ticks;
|
|
string JsonResult = "[{";
|
|
List<TResult> ListModel = new List<TResult>();
|
|
foreach (var model in ListModel)
|
|
{
|
|
foreach (var item in pList)
|
|
{
|
|
if (ListSerializePropertieName.Contains(item.Name) == false) continue;
|
|
object value = item.GetValue(model, null);
|
|
if (value == null)
|
|
{
|
|
JsonResult += "\"" + item.Name + "\":null,";
|
|
continue;
|
|
}
|
|
Type columnType = item.PropertyType;
|
|
if (item.PropertyType.IsGenericType && item.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
|
|
{
|
|
columnType = item.PropertyType.GetGenericArguments()[0];
|
|
|
|
}
|
|
if (columnType == typeof(string))
|
|
{
|
|
string vvv = value.ToString();
|
|
vvv = vvv.Replace("\r\n", "\\r\\n").Replace("\n", "\\n");
|
|
JsonResult += "\"" + item.Name + "\":\"" + vvv + "\",";
|
|
continue;
|
|
}
|
|
if (columnType == typeof(DateTime))
|
|
{
|
|
DateTime datetime = Convert.ToDateTime(value);
|
|
JsonResult += "\"" + item.Name + "\":\"\\/Date(" + (long)((datetime.ToUniversalTime().Ticks - DatetimeMinTimeTicks) / 0x2710) + ")\\/\",";
|
|
continue;
|
|
}
|
|
if (columnType == typeof(bool))
|
|
{
|
|
JsonResult += "\"" + item.Name + "\":" + value.ToString().ToLower() + ",";
|
|
continue;
|
|
}
|
|
if (columnType == typeof(Int64))
|
|
{
|
|
JsonResult += "\"" + item.Name + "\":\"" + value + "\",";
|
|
continue;
|
|
}
|
|
if (columnType == typeof(decimal))
|
|
{
|
|
JsonResult += "\"" + item.Name + "\":\"" + Convert.ToDecimal(value).ToString("0.#########") + "\",";
|
|
continue;
|
|
}
|
|
if (columnType == typeof(Int32) || columnType == typeof(Int16) || columnType == typeof(float) || columnType == typeof(short))
|
|
{
|
|
JsonResult += "\"" + item.Name + "\":" + value + ",";
|
|
continue;
|
|
}
|
|
JsonResult += "\"" + item.Name + "\":" + parseJSON.Serialize(value) + ",";
|
|
}
|
|
}
|
|
return JsonResult;
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
}
|
|
|
|
|