C#.NET Object扩展 ToJson/Serialize/Deserialize
发表日期:2010-8-18
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Data; using System.Data.Common; using System.Web.Script.Serialization; using System.IO; using System.Security.Cryptography; using System.ComponentModel; using System.Runtime.Serialization.Formatters.Binary; using System.Xml.Serialization;
namespace Pub.Class { public static class ObjectExtensions {
public static string ToJson(this object obj){ return ToJson(obj, null); } public static string ToJson(this object obj, IEnumerable<javaScriptConverter> jsonConverters) { JavascriptSerializer serializer = new JavaScriptSerializer(); if (jsonConverters != null) serializer.RegisterConverters(jsonConverters ?? new JavaScriptConverter[0]); return serializer.Serialize(obj); }
public static T ConvertTo<T>(this object value) { return value.ConvertTo(default(T)); } public static T ConvertTo<T>(this object value, T defaultValue) { if(value != null) { var targetType = typeof(T);
var converter = TypeDescriptor.GetConverter(value); if(converter != null) { if(converter.CanConvertTo(targetType)) return (T) converter.ConvertTo(value, targetType); }
converter = TypeDescriptor.GetConverter(targetType); if(converter != null) { try { if(converter.CanConvertFrom(value.GetType())) return (T) converter.ConvertFrom(value); } catch {} } } return defaultValue; } public static T ConvertTo<T>(this object value, T defaultValue, bool ignoreException) { if(ignoreException) { try { return value.ConvertTo<T>(); } catch { return defaultValue; } } return value.ConvertTo<T>(); }
public static int ToInt(this object strValue, int defValue) { int def = 0; int.TryParse(strValue.ToString(), out def); return def == 0 ? defValue : def; } public static byte ToTinyInt(this object strValue, byte defValue) { byte def = 0; byte.TryParse(strValue.ToString(), out def); return def == 0 ? defValue : def; } public static short ToSmallInt(this object strValue, short defValue) { short def = 0; short.TryParse(strValue.ToString(), out def); return def == 0 ? defValue : def; } public static decimal ToDecimal(this object strValue, decimal defValue) { decimal def = 0; decimal.TryParse(strValue.ToString(), out def); return def == 0 ? defValue : def; } public static float ToFloat(this object strValue, float defValue) { float def = 0; float.TryParse(strValue.ToString(), out def); return def == 0 ? defValue : def; } public static Int64 ToBigInt(this object strValue, Int64 defValue) { Int64 def = 0; Int64.TryParse(strValue.ToString(), out def); return def == 0 ? defValue : def; } public static decimal ToMoney(this object strValue, decimal defValue) { decimal def = 0; decimal.TryParse(strValue.ToString(), out def); return def == 0 ? defValue : def; } public static int ToInteger(this object strValue, int defValue) { int def = 0; int.TryParse(strValue.ToString(), out def); return def == 0 ? defValue : def; } public static bool ToBool(this object ExPRession, bool defValue) { if (Expression != null) { if (string.Compare(Expression.ToString(), "true", true) == 0) return true; if (string.Compare(Expression.ToString(), "false", true) == 0) return false; if (string.Compare(Expression.ToString(), "1", true) == 0) return true; if (string.Compare(Expression.ToString(), "0", true) == 0) return false; } return defValue; } public static int ToInt(this object strValue) { return strValue.ToInt(0); } public static byte ToTinyInt(this object strValue) { return strValue.ToTinyInt(0); } public static short ToSmallInt(this object strValue) { return strValue.ToSmallInt(0); } public static decimal ToDecimal(this object strValue) { return strValue.ToDecimal(0); } public static float ToFloat(this object strValue) { return strValue.ToFloat(0); } public static Int64 ToBigInt(this object strValue) { return strValue.ToBigInt(0); } public static decimal ToMoney(this object strValue) { return strValue.ToMoney(0); } public static int ToInteger(this object strValue) { return strValue.ToInteger(0); } public static bool ToBool(this object strValue) { return strValue.ToBool(false); }
public static object InvokeMethod(this object obj, string methodName, params object[] parameters) { return InvokeMethod<object>(obj, methodName, parameters); } public static T InvokeMethod<T>(this object obj, string methodName) { return InvokeMethod<T>(obj, methodName, null); } public static T InvokeMethod<T>(this object obj, string methodName, params object[] parameters) { var type = obj.GetType(); var method = type.GetMethod(methodName);
if(method == null) throw new ArgumentException(string.Format("Method '{0}' not found.", methodName), methodName);
var value = method.Invoke(obj, parameters); return (value is T ? (T) value : default(T)); }
public static object GetPropertyValue(this object obj, string propertyName) { return GetPropertyValue<object>(obj, propertyName, null); } public static T GetPropertyValue<T>(this object obj, string propertyName) { return GetPropertyValue<T>(obj, propertyName, default(T)); } public static T GetPropertyValue<T>(this object obj, string propertyName, T defaultValue) { var type = obj.GetType(); var property = type.GetProperty(propertyName);
if(property == null) throw new ArgumentException(string.Format("Property '{0}' not found.", propertyName), propertyName);
var value = property.GetValue(obj, null); return (value is T ? (T) value : defaultValue); } public static void SetPropertyValue(this object obj, string propertyName, object value) { var type = obj.GetType(); var property = type.GetProperty(propertyName);
if(property == null) throw new ArgumentException(string.Format("Property '{0}' not found.", propertyName), propertyName);
property.SetValue(obj, value, null); }
public static T GetAttribute<T>(this object obj) where T : Attribute { return GetAttribute<T>(obj, true); } public static T GetAttribute<T>(this object obj, bool includeInherited) where T : Attribute { var type = (obj as Type ?? obj.GetType()); var attributes = type.GetCustomAttributes(typeof(T), includeInherited); if((attributes != null) && (attributes.Length > 0)) { return (attributes[0] as T); } return null; }
public static IEnumerable<T> GetAttributes<T>(this object obj) where T : Attribute { return GetAttributes<T>(obj); } public static IEnumerable<T> GetAttributes<T>(this object obj, bool includeInherited) where T : Attribute { var type = (obj as Type ?? obj.GetType()); foreach(var attribute in type.GetCustomAttributes(typeof(T), includeInherited)) { if(attribute is T) yield return (T) attribute; } }
public static bool IsType(this object obj, Type type) { return obj.GetType().Equals(type); } public static T ToType<T>(this object value) { return (T)value; } public static bool IsArray(this object obj) { return obj.IsType(typeof(System.Array)); } public static bool IsDBNull(this object obj) { return obj.IsType(typeof(DBNull)); }
public static byte[] Serialize(this object value) { MemoryStream ms = new MemoryStream(); BinaryFormatter bf1 = new BinaryFormatter(); bf1.Serialize(ms, value); return ms.ToArray(); }
public static void CheckOnNull(this object @this, string parameterName) { if(@this.IsNull()) throw new ArgumentNullException(parameterName); } public static void CheckOnNull(this object @this, string parameterName, string message) { if(@this.IsNull()) throw new ArgumentNullException(parameterName, message); } public static bool IsNull(this object @this) { return @this == null; } public static bool IsNotNull(this object @this) { return !@this.IsNull(); } public static T UnsafeCast<T>(this object value) { return value.IsNull() ? default(T) : (T)value; } public static T SafeCast<T>(this object value) { return value is T ? value.UnsafeCast<T>() : default(T); } public static bool InstanceOf<T>(this object value) { return value is T; }
public static void SerializeXmlFile(this object o, string fileName) { XmlSerializer serializer = new XmlSerializer(o.GetType()); if (!FileFolder.FileExists(fileName)) return; using (FileStream stream = new FileStream(fileName, FileMode.Create, Fileaccess.Write)) serializer.Serialize(stream, o); } public static T DeserializeXmlFile<T>(string fileName) { T o; XmlSerializer serializer = new XmlSerializer(typeof(T)); using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) o = (T)serializer.Deserialize(stream); return o; }
public static string SerializeXml(this object o) { XmlSerializer serializer = new XmlSerializer(o.GetType()); StringBuilder stringBuilder = new StringBuilder(); using (TextWriter textWriter = new StringWriter(stringBuilder)) serializer.Serialize(textWriter, o); return stringBuilder.ToString(); } public static T DeserializeXml<T>(this string xml) { return (T)Deserialize(xml, typeof(T)); } public static object Deserialize(string xml, Type type) { object o; XmlSerializer serializer = new XmlSerializer(type); using (TextReader textReader = new StringReader(xml)) o = serializer.Deserialize(textReader); return o; }
public static void Write(this object o) { Msg.Write(o); } public static void WriteEnd(this object o) { Msg.WriteEnd(o); } } } |
|
|
|
上一篇:初步理解正则表达式中的Lookaround
人气:2985
下一篇:c# 中的静态类(satic class)和具体类的区别及用法
人气:1031 |
|
|
网站文章搜索
|
 |
|
邮件订阅服务
|
 |
|
今日更新文章
|
 |
·PS打造清爽艳丽的海景婚片
·Photoshop打造漂亮的黄绿色非主流MM
·PS调出MM秀丽清爽的色彩
·Photoshop打造洁白如玉的完美肌肤
·Photoshop给MM打造一幅光环艺术照
·Photoshop制作个性青黄色非主流效果
·PS给人物照片添加艺术背景
·Photoshop打造柔美的紫黄色时装美女图片
·Photoshop加强人像图片的质感并增加梦幻
·怎样让新站从开始就拥有高权重
·SEO市场的高端盈利模式详细分解
·站内高质量原创文章有利于网站优化
|
本栏目推荐文章
|
 |
·在Java编程语言中实现UDP协议编程的方法
·“6sigma”反面思维探讨
·从ASP迁移至ASP+ --HTML表格转换为ASP+列
·AJAX也有安全隐患 谈谈AJAX的安全性
·网页表单自动填写技术(gmail为例)
·关于对象持久类框架的构架设计(Part1)
·深入掌握Java技术 EJB调用原理分析(1)
·回复编程爱好者请教的有关题二叉树的创建
·C语言库函数 (B类字母)
·jsp源码实例5(cookie)
·PHP中的正规表达式(二)
·让Fastreport3.x支持中文PDF的输出
|