123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Runtime;
- using System.Runtime.InteropServices;
- using System.Text.RegularExpressions;
- using System.Web;
- namespace SiwiFms.Helper
- {
- public static class HelpTo
- {
- /// <summary>
- /// 将对象转json字符串
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string ToJson(this object obj)
- {
- return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
- }
- /// <summary>
- /// 将string Json字符串转实体类型
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="str"></param>
- /// <returns></returns>
- public static T ToEntity<T>(this string str)
- {
- return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(str);
- }
-
- /// <summary>
- /// 将对象转Int
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static int ToInt(this object str)
- {
- if (str == null)
- return 0;
- else
- return Convert.ToInt32(str);
- }
- /// <summary>
- /// 将对象转double类型
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static double ToDouble(this object str)
- {
- if (str == null)
- return 0;
- else
- {
- double tmp;
- bool bl= double.TryParse(str.ToStr(), out tmp);
- if (bl)
- return tmp;
- else
- return 0;
- }
- }
- /// <summary>
- /// 是否是Int类型
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static bool IsInt(this object obj)
- {
- int tmp;
- return int.TryParse(obj.ToString(), out tmp);
- }
- /// <summary>
- /// 将对象转string
- /// </summary>
- /// <param name="t"></param>
- /// <returns></returns>
- public static string ToStr(this object t)
- {
- if (t == null)
- return string.Empty;
- else
- return t.ToString();
- }
- /// <summary>
- /// 是否为空
- /// </summary>
- /// <param name="t"></param>
- /// <returns></returns>
- public static bool IsNull(this object t)
- {
- if (t == null)
- return true;
- else
- return false;
- }
- public static bool IsNullOrEmpty(this string str)
- {
- if (string.IsNullOrEmpty(str))
- return true;
- else
- return false;
- }
- /// <summary>
- /// 获取新的ID
- /// </summary>
- public static string NewId
- {
- get {
- return Guid.NewGuid().ToString();
- }
- }
- /// <summary>
- /// 获取新的时间
- /// </summary>
- public static DateTime NewDate
- {
- get {
- return DateTime.Now;
- }
- }
- /// <summary>
- /// 将对象转时间类型
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static DateTime ToDateTime(this string str)
- {
- DateTime dt;
- if (DateTime.TryParse(str, out dt))
- {
- return dt;
- }
- else
- {
- return DateTime.MinValue;//返回最小的时间
- }
- }
- /// <summary>
- /// 将对象反射赋值
- /// </summary>
- /// <typeparam name="S"></typeparam>
- /// <typeparam name="T"></typeparam>
- /// <param name="s">原对象</param>
- /// <param name="t">转换后的对象</param>
- /// <returns></returns>
- public static T AutoMapping<T>(this object s) where T : new()
- {
- T t=new T();
- ObjectReflection.AutoMapping<T>(s,t);
- return t;
- }
- /// <summary>
- /// 将对象集合反射赋值
- /// </summary>
- /// <typeparam name="S"></typeparam>
- /// <typeparam name="T">原集合</typeparam>
- /// <param name="s">转换后集合</param>
- /// <returns></returns>
- public static List<T> AutoMapping<S, T>(this List<S> s) where T : new()
- {
- List<T> t = new List<T>();
- ObjectReflection.AutoMapping<S,T>(s, t);
- return t;
- }
- //获取枚举描述信息
- public static string GetDescription(this Enum value)
- {
- Type type = value.GetType();
- string name = Enum.GetName(type, value);
- if (!name.IsNull())
- {
- FieldInfo fieldInfo = type.GetField(name);
- if (!fieldInfo.IsNull())
- {
- DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
- if (!attr.IsNull())
- {
- return attr.Description;
- }
- }
- }
- return null;
- }
- }
- }
|