using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
namespace Helpers
{
static public class MyConvert
{
///
/// 其他类型转json字符串
///
/// 待转换的对象
///
public static string ToJson(this object obj)
{
if (obj == null)
{
return null;
}
return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
}
///
/// json转model
///
/// 转换后的类型
/// json字符串
///
public static T ToModel(this string str)
{
if (!string.IsNullOrWhiteSpace(str))
{
var res = Newtonsoft.Json.JsonConvert.DeserializeObject(str);
if (res == null) {
return default;
} else
{
return res;
}
}
return default;
}
///
/// 其他类型转model
///
/// 转换后的对象
/// 待转换的对象
///
public static T ToModel(this object obj)
{
if (obj == null)
{
return default(T);
}
string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
T model = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
return model;
}
///
/// 其他类型转DataTable
///
/// 待转换的对象
///
public static DataTable ToTable(this object obj)
{
if (obj == null)
{
return null;
}
string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
return Newtonsoft.Json.JsonConvert.DeserializeObject(json);
}
///
/// json转DataTable
///
/// json字符串
///
public static DataTable ToTable(this string str)
{
if (!string.IsNullOrWhiteSpace(str))
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(str);
}
return default(DataTable);
}
///
/// 字符串转整形
///
/// 数值字符串
///
/// 异常:非法的数据
public static int ToInt(this string str)
{
int resInt = 0;
if (int.TryParse(str, out resInt))
{
return resInt;
}
else
{
throw new Exception("该字符串无法转换成整形");
}
}
///
/// 其他类型转整形
///
/// 数值对象
///
/// 异常:非法的数据
public static int ToInt(this object str)
{
int resInt = 0;
if (int.TryParse(str.ToString(), out resInt))
{
return resInt;
}
else
{
throw new Exception("该字符串无法转换成整形");
}
}
///
/// datetime转字符串(yyyy-MM-dd HH:mm:ss.fff)
///
/// 待转换的时间
///
public static string GetTime(this DateTime dt)
{
return dt.ToString("yyyy-MM-dd HH:mm:ss.fff");
}
///
/// 其他类型转时间
///
/// 待转换的对象
///
///
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("非法字符串");
}
}
///
/// 字符串转时间
///
/// 待转换的字符串对象
///
public static DateTime FormatDate(this string str)
{
return DateTime.Parse(str);
}
///
/// 时间转固定格式字符串
///
/// 等转换的时间
///
public static string ToTimeStr(this DateTime dt)
{
return GetTime(dt);
}
///
/// 对象转日期字符串(yyyy-MM-dd)
///
/// 待转换的对象
///
///
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("无法转换的内容");
}
}
///
/// 流转Byte数组
///
/// 待转换的流
///
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;
}
///
/// Byte数组转流
///
///
///
public static Stream ToStream(this byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
///
/// json转list数组
///
/// List中的泛型对象类型
/// 待转换的json字符串
///
public static List ToList(this string str)
{
if (!string.IsNullOrWhiteSpace(str))
{
return Newtonsoft.Json.JsonConvert.DeserializeObject>(str);
}
return default(List);
}
///
/// 其他类型转List
///
/// List中的泛型对象类型
/// 待转换的对象
///
public static List ToList(this object obj)
{
string str = obj.ToJson();
if (!string.IsNullOrWhiteSpace(str))
{
return Newtonsoft.Json.JsonConvert.DeserializeObject>(str);
}
return default(List);
}
///
/// DataTable首列转List
///
/// 待转换的Datatable
///
public static List ToList(this DataTable dt)
{
List res = new List();
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;
}
///
/// 将List转成DataTable
///
/// 新列的数据
/// 列名
///
public static DataTable ToTable(this List 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;
}
///
/// 将DataTable中的空行移除
///
/// 待处理的DataTable
///
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;
}
}
}