• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#枚举和object对象拓展代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1,枚举拓展
/// <summary>
/// 获取枚举描述
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumItemName"></param>
/// <returns></returns>
public static string GetDescriptionByName<T>(this T enumItemName)
{
var fi = enumItemName.GetType().GetField(enumItemName.ToString());

var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);

if (attributes.Length > 0)
return attributes[0].Description;

return enumItemName.ToString();
}

2,object拓展:


/// <summary>
///
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static bool Equal<T>(this T x, T y)
{
return ((IComparable)(x)).CompareTo(y) == 0;
}
#region 数值转换
/// <summary>
/// 转换为整型
/// </summary>
/// <param name="data">数据</param>
public static int ToInt(this object data)
{
if (data == null)
return 0;
int result;
var success = int.TryParse(data.ToString(), out result);
if (success)
return result;
try
{
return Convert.ToInt32(ToDouble(data, 0));
}
catch (Exception)
{
return 0;
}
}

/// <summary>
/// 转换为可空整型
/// </summary>
/// <param name="data">数据</param>
public static int? ToIntOrNull(this object data)
{
if (data == null)
return null;
int result;
var isValid = int.TryParse(data.ToString(), out result);
if (isValid)
return result;
return null;
}

/// <summary>
/// 转换为双精度浮点数
/// </summary>
/// <param name="data">数据</param>
public static double ToDouble(this object data)
{
if (data == null)
return 0;
double result;
return double.TryParse(data.ToString(), out result) ? result : 0;
}

/// <summary>
/// 转换为双精度浮点数,并按指定的小数位4舍5入
/// </summary>
/// <param name="data">数据</param>
/// <param name="digits">小数位数</param>
public static double ToDouble(this object data, int digits)
{
return Math.Round(ToDouble(data), digits);
}

/// <summary>
/// 转换为可空双精度浮点数
/// </summary>
/// <param name="data">数据</param>
public static double? ToDoubleOrNull(this object data)
{
if (data == null)
return null;
double result;
var isValid = double.TryParse(data.ToString(), out result);
if (isValid)
return result;
return null;
}

/// <summary>
/// 转换为高精度浮点数
/// </summary>
/// <param name="data">数据</param>
public static decimal ToDecimal(this object data)
{
if (data == null)
return 0;
decimal result;
return decimal.TryParse(data.ToString(), out result) ? result : 0;
}

/// <summary>
/// 转换为高精度浮点数,并按指定的小数位4舍5入
/// </summary>
/// <param name="data">数据</param>
/// <param name="digits">小数位数</param>
public static decimal ToDecimal(this object data, int digits)
{
return Math.Round(ToDecimal(data), digits);
}

/// <summary>
/// 转换为可空高精度浮点数
/// </summary>
/// <param name="data">数据</param>
public static decimal? ToDecimalOrNull(this object data)
{
if (data == null)
return null;
decimal result;
var isValid = decimal.TryParse(data.ToString(), out result);
if (isValid)
return result;
return null;
}

/// <summary>
/// 转换为可空高精度浮点数,并按指定的小数位4舍5入
/// </summary>
/// <param name="data">数据</param>
/// <param name="digits">小数位数</param>
public static decimal? ToDecimalOrNull(this object data, int digits)
{
var result = ToDecimalOrNull(data);
if (result == null)
return null;
return Math.Round(result.Value, digits);
}

#endregion
/// <summary>
/// 将一个object对象序列化,返回一个byte[]
/// </summary>
/// <param name="obj">能序列化的对象</param>
/// <returns></returns>
public static byte[] ToBytes(this object obj)
{
using (var ms = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
return ms.GetBuffer();
}
}


/// <summary>
/// 判断是否为Null或者空
/// </summary>
/// <param name="obj">对象</param>
/// <returns></returns>
public static bool IsNullOrEmpty(this object obj)
{
if (obj == null)
return true;
else if (obj is ICollection)
{
var list = obj as ICollection;

return list.Count <= 0;
}
else
{
var objStr = obj.ToString();
return string.IsNullOrEmpty(objStr);
}
}

/// <summary>
/// 实体类转json数据,速度快
/// </summary>
/// <param name="t">实体类</param>
/// <returns></returns>
public static string EntityToJson(this object t)
{
if (t == null)
return null;
var jsonStr = "";
jsonStr += "{";
var infos = t.GetType().GetProperties();
for (var i = 0; i < infos.Length; i++)
{
jsonStr = jsonStr + "\"" + infos[i].Name + "\":\"" + infos[i].GetValue(t) as string + "\"";
if (i != infos.Length - 1)
jsonStr += ",";
}
jsonStr += "}";
return jsonStr;
}

/// <summary>
/// 深复制
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="obj">对象</param>
/// <returns></returns>
public static T DeepClone<T>(this T obj) where T : class
{
if (obj == null)
return null;

return obj.ToJson().ToObject<T>();
}

/// <summary>
/// 将对象序列化为XML字符串
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="obj">对象</param>
/// <returns></returns>
public static string ToXmlStr<T>(this T obj)
{
var jsonStr = obj.ToJson();
var xmlDoc = JsonConvert.DeserializeXmlNode(jsonStr);
var xmlDocStr = xmlDoc.InnerXml;

return xmlDocStr;
}

/// <summary>
/// 将对象序列化为XML字符串
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="obj">对象</param>
/// <param name="rootNodeName">根节点名(建议设为xml)</param>
/// <returns></returns>
public static string ToXmlStr<T>(this T obj, string rootNodeName)
{
var jsonStr = obj.ToJson();
var xmlDoc = JsonConvert.DeserializeXmlNode(jsonStr, rootNodeName);
var xmlDocStr = xmlDoc.InnerXml;

return xmlDocStr;
}

/// <summary>
/// 取得对象指定属性的值
/// </summary>
/// <param name="predicate">要取值的属性</param>
/// <returns></returns>
public static object GetPropertyValue<T, TProperty>(this T obj, Expression<Func<T, TProperty>> predicate)
{
var propertyName = predicate.GetPropertyName(); //属性名称

return obj.GetPropertyValue(propertyName);
}

/// <summary>
/// 取对象属性值
/// </summary>
/// <param name="obj"></param>
/// <param name="propertyName">支持“.”分隔的多级属性取值。</param>
/// <returns></returns>
public static object GetPropertyValue<T>(this T obj, string propertyName)
{
var strs = propertyName.Split('.');

PropertyInfo property = null;
object value = obj;

for (var i = 0; i < strs.Length; i++)
{
property = value.GetType().GetProperty(strs[i]);
value = property.GetValue(value, null);
}
return value;
}

/// <summary>
/// 设置对象指定属性的值
/// </summary>
/// <param name="predicate">要设置值的属性</param>
/// <param name="value">设置值</param>
/// <returns>是否设置成功</returns>
public static bool SetPropertyValue<T, TProperty>(this T obj, Expression<Func<T, TProperty>> predicate,
object value)
{
var propertyName = predicate.GetPropertyName(); //属性名称

return obj.SetPropertyValue(propertyName, value);
}

/// <summary>
/// 设置对象属性值
/// </summary>
/// <param name="obj"></param>
/// <param name="propertyName">propertyName1.propertyName2.propertyName3</param>
/// <param name="value"></param>
/// <returns></returns>
public static bool SetPropertyValue<T>(this T obj, string propertyName, object value)
{
var strs = propertyName.Split('.');

PropertyInfo property = null;
object target = obj;

for (var i = 0; i < strs.Length; i++)
{
property = target.GetType().GetProperty(strs[i]);
if (i < strs.Length - 1)
target = property.GetValue(target, null);
}

var flag = false; //设置成功标记
if (property != null && property.CanWrite)
{
if (false == property.PropertyType.IsGenericType) //非泛型
{
if (property.PropertyType.IsEnum)
{
property.SetValue(target, Convert.ChangeType(value, typeof(int)));
flag = true;
}
else if (value.ToString() != property.PropertyType.ToString())
{
//property.SetValue(target, string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, property.PropertyType), null);
property.SetValue(target,
value == null ? null : Convert.ChangeType(value, property.PropertyType),
null);
flag = true;
}
}
else //泛型Nullable<>
{
var genericTypeDefinition = property.PropertyType.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>))
{
//property.SetValue(target, string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, Nullable.GetUnderlyingType(property.PropertyType)), null);
property.SetValue(target,
value == null
? null
: Convert.ChangeType(value, Nullable.GetUnderlyingType(property.PropertyType)),
null);
flag = true;
}
}
}

return flag;
}
/// <summary>
/// 将对象序列化成Json字符串
/// </summary>
/// <param name="obj">需要序列化的对象</param>
/// <returns></returns>
//public static string ToJson(this object obj)
//{
// return JsonConvert.SerializeObject(obj);
//}

//public static object ToJson(this string Json)
//{
// return Json == null ? null : JsonConvert.DeserializeObject(Json);
//}
public static string ToJson(this object obj)
{
if(obj == null)
{
return "";
}

var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
return JsonConvert.SerializeObject(obj, timeConverter);
}
public static string ToJson(this object obj, string datetimeformats)
{
if (obj == null)
{
return "";
}
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = datetimeformats };
return JsonConvert.SerializeObject(obj, timeConverter);
}
//public static T ToObject<T>(this string Json)
//{
// return Json == null ? default(T) : JsonConvert.DeserializeObject<T>(Json);
//}
//public static List<T> ToList<T>(this string Json)
//{
// return Json == null ? null : JsonConvert.DeserializeObject<List<T>>(Json);
//}
public static DataTable ToTable(this string json)
{
return json == null ? null : JsonConvert.DeserializeObject<DataTable>(json);
}
public static JObject ToJObject(this string json)
{
return json == null ? JObject.Parse("{}") : JObject.Parse(json.Replace("&nbsp;", ""));
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++11新特性之线程操作发布时间:2022-07-14
下一篇:
C#LinQ语法发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap