using System;
|
using System.Collections.Generic;
|
using System.Data;
|
using System.IO;
|
using System.Text;
|
|
namespace Helpers
|
{
|
static public class MyConvert
|
{
|
/// <summary>
|
/// 其他类型转json字符串
|
/// </summary>
|
/// <param name="obj">待转换的对象</param>
|
/// <returns></returns>
|
public static string ToJson(this object obj)
|
{
|
if (obj == null)
|
{
|
return null;
|
}
|
return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
|
}
|
/// <summary>
|
/// json转model
|
/// </summary>
|
/// <typeparam name="T">转换后的类型</typeparam>
|
/// <param name="str">json字符串</param>
|
/// <returns></returns>
|
public static T ToModel<T>(this string str)
|
{
|
if (!string.IsNullOrWhiteSpace(str))
|
{
|
var res = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(str);
|
if (res == null) {
|
return default;
|
} else
|
{
|
return res;
|
}
|
}
|
return default;
|
}
|
/// <summary>
|
/// 其他类型转model
|
/// </summary>
|
/// <typeparam name="T">转换后的对象</typeparam>
|
/// <param name="obj">待转换的对象</param>
|
/// <returns></returns>
|
public static T ToModel<T>(this object obj)
|
{
|
if (obj == null)
|
{
|
return default(T);
|
}
|
string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
|
T model = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
|
return model;
|
}
|
/// <summary>
|
/// 其他类型转DataTable
|
/// </summary>
|
/// <param name="obj">待转换的对象</param>
|
/// <returns></returns>
|
public static DataTable ToTable(this object obj)
|
{
|
if (obj == null)
|
{
|
return null;
|
}
|
string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
|
return Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(json);
|
}
|
/// <summary>
|
/// json转DataTable
|
/// </summary>
|
/// <param name="str">json字符串</param>
|
/// <returns></returns>
|
public static DataTable ToTable(this string str)
|
{
|
if (!string.IsNullOrWhiteSpace(str))
|
{
|
return Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>(str);
|
}
|
return default(DataTable);
|
}
|
/// <summary>
|
/// 字符串转整形
|
/// </summary>
|
/// <param name="str">数值字符串</param>
|
/// <returns></returns>
|
/// <exception cref="Exception">异常:非法的数据</exception>
|
public static int ToInt(this string str)
|
{
|
int resInt = 0;
|
if (int.TryParse(str, out resInt))
|
{
|
return resInt;
|
}
|
else
|
{
|
throw new Exception("该字符串无法转换成整形");
|
}
|
}
|
/// <summary>
|
/// 其他类型转整形
|
/// </summary>
|
/// <param name="str">数值对象</param>
|
/// <returns></returns>
|
/// <exception cref="Exception">异常:非法的数据</exception>
|
public static int ToInt(this object str)
|
{
|
int resInt = 0;
|
if (int.TryParse(str.ToString(), out resInt))
|
{
|
return resInt;
|
}
|
else
|
{
|
throw new Exception("该字符串无法转换成整形");
|
}
|
}
|
/// <summary>
|
/// datetime转字符串(yyyy-MM-dd HH:mm:ss.fff)
|
/// </summary>
|
/// <param name="dt">待转换的时间</param>
|
/// <returns></returns>
|
public static string GetTime(this DateTime dt)
|
{
|
return dt.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
}
|
/// <summary>
|
/// 其他类型转时间
|
/// </summary>
|
/// <param name="obj">待转换的对象</param>
|
/// <returns></returns>
|
/// <exception cref="Exception"></exception>
|
public static DateTime ToDateTime(this object obj)
|
{
|
if (obj == null)
|
{
|
throw new Exception("对象不能为空");
|
}
|
string str = obj.ToString();
|
DateTime dt = DateTime.Now;
|
if (DateTime.TryParse(str, out dt))
|
{
|
return dt;
|
}
|
else
|
{
|
throw new Exception("非法字符串");
|
}
|
}
|
/// <summary>
|
/// 字符串转时间
|
/// </summary>
|
/// <param name="str">待转换的字符串对象</param>
|
/// <returns></returns>
|
public static DateTime FormatDate(this string str)
|
{
|
return DateTime.Parse(str);
|
}
|
/// <summary>
|
/// 时间转固定格式字符串
|
/// </summary>
|
/// <param name="dt">等转换的时间</param>
|
/// <returns></returns>
|
public static string ToTimeStr(this DateTime dt)
|
{
|
return GetTime(dt);
|
}
|
/// <summary>
|
/// 对象转日期字符串(yyyy-MM-dd)
|
/// </summary>
|
/// <param name="obj">待转换的对象</param>
|
/// <returns></returns>
|
/// <exception cref="Exception"></exception>
|
public static string GetDate(this object obj)
|
{
|
if (obj == null)
|
{
|
return null;
|
}
|
string str = obj.ToString();
|
DateTime dt = DateTime.Now;
|
if (DateTime.TryParse(str, out dt))
|
{
|
return dt.ToString("yyyy-MM-dd");
|
}
|
else
|
{
|
throw new Exception("无法转换的内容");
|
}
|
}
|
/// <summary>
|
/// 流转Byte数组
|
/// </summary>
|
/// <param name="stream">待转换的流</param>
|
/// <returns></returns>
|
public static byte[] ToByte(this Stream stream)
|
{
|
byte[] bytes = new byte[stream.Length];
|
stream.Read(bytes, 0, bytes.Length);
|
|
// 设置当前流的位置为流的开始
|
stream.Seek(0, SeekOrigin.Begin);
|
return bytes;
|
}
|
/// <summary>
|
/// Byte数组转流
|
/// </summary>
|
/// <param name="bytes"></param>
|
/// <returns></returns>
|
public static Stream ToStream(this byte[] bytes)
|
{
|
Stream stream = new MemoryStream(bytes);
|
return stream;
|
}
|
/// <summary>
|
/// json转list数组
|
/// </summary>
|
/// <typeparam name="T">List<T>中的泛型对象类型</typeparam>
|
/// <param name="str">待转换的json字符串</param>
|
/// <returns></returns>
|
public static List<T> ToList<T>(this string str)
|
{
|
if (!string.IsNullOrWhiteSpace(str))
|
{
|
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<T>>(str);
|
}
|
return default(List<T>);
|
}
|
/// <summary>
|
/// 其他类型转List
|
/// </summary>
|
/// <typeparam name="T">List<T>中的泛型对象类型</typeparam>
|
/// <param name="obj">待转换的对象</param>
|
/// <returns></returns>
|
public static List<T> ToList<T>(this object obj)
|
{
|
string str = obj.ToJson();
|
if (!string.IsNullOrWhiteSpace(str))
|
{
|
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<T>>(str);
|
}
|
return default(List<T>);
|
}
|
/// <summary>
|
/// DataTable首列转List<string>
|
/// </summary>
|
/// <param name="dt">待转换的Datatable</param>
|
/// <returns></returns>
|
public static List<string> ToList(this DataTable dt)
|
{
|
List<string> res = new List<string>();
|
if (dt == null || dt.Rows == null || dt.Rows.Count == 0) { return res; }
|
foreach (DataRow dr in dt.Rows)
|
{
|
if (string.IsNullOrWhiteSpace(Convert.ToString(dr[0]))) { continue; }
|
res.Add(Convert.ToString(dr[0]));
|
}
|
return res;
|
}
|
/// <summary>
|
/// 将List<string>转成DataTable
|
/// </summary>
|
/// <param name="list">新列的数据</param>
|
/// <param name="ColumnName">列名</param>
|
/// <returns></returns>
|
public static DataTable ToTable(this List<string> list, string ColumnName)
|
{
|
DataTable res = new DataTable();
|
res.Columns.Add(ColumnName);
|
if (list == null || list.Count == 0) { return res; }
|
foreach (var a in list)
|
{
|
if (string.IsNullOrWhiteSpace(a)) { continue; }
|
var dr = res.NewRow();
|
dr[0] = a;
|
res.Rows.Add(dr);
|
}
|
return res;
|
}
|
/// <summary>
|
/// 将DataTable中的空行移除
|
/// </summary>
|
/// <param name="dt">待处理的DataTable</param>
|
/// <returns></returns>
|
public static DataTable RemoveEmpty(this DataTable dt)
|
{
|
int count = dt.Rows.Count - 1;
|
while (count >= 0)
|
{
|
bool b = true;
|
foreach (var a in dt.Rows[count].ItemArray)
|
{
|
if (!string.IsNullOrWhiteSpace(a.ToString()))
|
{
|
b = b & false;
|
break;
|
}
|
}
|
if (b)
|
{
|
dt.Rows.Remove(dt.Rows[count]);
|
}
|
count--;
|
}
|
return dt;
|
}
|
}
|
}
|