utiliser le système ;
en utilisant System.Collections.Generic ;
en utilisant System.Linq ;
en utilisant System.Text ;
en utilisant System.Reflection ;
en utilisant System.Data ;
en utilisant System.Data.Common ;
en utilisant System.Web.Script.Serialization ;
en utilisant System.IO ;
en utilisant System.Security.Cryptography ;
en utilisant System.ComponentModel ;
en utilisant System.Runtime.Serialization.Formatters.Binary ;
en utilisant System.xml.Serialization ;
espace de noms Pub.Class {
classe statique publique ObjectExtensions {
chaîne statique publique ToJson (cet objet obj) {
retourner ToJson(obj, null);
}
chaîne statique publique ToJson (cet objet obj, IEnumerable<javaScriptConverter> jsonConverters) {
Sérialiseur JavascriptSerializer = new JavaScriptSerializer();
if (jsonConverters != null) serializer.RegisterConverters(jsonConverters ?? new JavaScriptConverter[0]);
retourner sérialiseur.Serialize(obj);
}
public static T ConvertTo<T>(cette valeur d'objet) { return value.ConvertTo(default(T)); }
public static T ConvertTo<T>(cette valeur d'objet, T defaultValue) {
si(valeur != null) {
var targetType = typeof(T);
var convertisseur = TypeDescriptor.GetConverter (valeur);
if(convertisseur != null) {
if(converter.CanConvertTo(targetType)) return (T) converter.ConvertTo(value, targetType);
}
convertisseur = TypeDescriptor.GetConverter(targetType);
if(convertisseur != null) {
essayez { if(converter.CanConvertFrom(value.GetType())) return (T) converter.ConvertFrom(value); } attraper {}
}
}
renvoie la valeur par défaut ;
}
public static T ConvertTo<T>(cette valeur d'objet, T defaultValue, bool ignoreException) {
si (ignorerException) {
essayer {
valeur de retour.ConvertTo<T>();
}
attraper {
renvoie la valeur par défaut ;
}
}
valeur de retour.ConvertTo<T>();
}
public static int ToInt (cet objet strValue, int defValue) { int def = 0; int.TryParse(strValue.ToString(), out def); return def == 0 ? valeur def : déf; }
octet statique public ToTinyInt (cet objet strValue, octet defValue) { octet def = 0 ; byte.TryParse(strValue.ToString(), out def); return def == 0 ? valeur def : déf; }
public static short ToSmallInt (cet objet strValue, short defValue) { short def = 0; short.TryParse(strValue.ToString(), out def); return def == 0 ? valeur def : déf; }
public static decimal ToDecimal (cet objet strValue, decimal defValue) { decimal def = 0; decimal.TryParse(strValue.ToString(), out def); return def == 0 ? valeur def : déf; }
public static float ToFloat (cet objet strValue, float defValue) { float def = 0; float.TryParse(strValue.ToString(), out def); return def == 0 ? valeur def : déf; }
public static Int64 ToBigInt (cet objet strValue, Int64 defValue) { Int64 def = 0; Int64.TryParse(strValue.ToString(), out def); return def == 0 ? valeur def : déf; }
public static decimal ToMoney (cet objet strValue, decimal defValue) { decimal def = 0; decimal.TryParse(strValue.ToString(), out def); return def == 0 ? valeur def : déf; }
public static int ToInteger (cet objet strValue, int defValue) { int def = 0; int.TryParse(strValue.ToString(), out def); return def == 0 ? valeur def : déf; }
public static bool ToBool (cet objet ExPRession, bool defValue) {
si (Expression != null) {
if (string.Compare(Expression.ToString(), "true", true) == 0) renvoie vrai ;
if (string.Compare(Expression.ToString(), "false", true) == 0) renvoie false ;
if (string.Compare(Expression.ToString(), "1", true) == 0) renvoie vrai ;
if (string.Compare(Expression.ToString(), "0", true) == 0) renvoie false ;
}
renvoyer defValue ;
}
public static int ToInt (cet objet strValue) { return strValue.ToInt (0); }
octet statique public ToTinyInt (cet objet strValue) { return strValue.ToTinyInt (0); }
public static short ToSmallInt (cet objet strValue) { return strValue.ToSmallInt (0); }
public static decimal ToDecimal (cet objet strValue) { return strValue.ToDecimal (0); }
public static float ToFloat (cet objet strValue) { return strValue.ToFloat (0); }
public static Int64 ToBigInt (cet objet strValue) { return strValue.ToBigInt (0); }
public static decimal ToMoney (cet objet strValue) { return strValue.ToMoney (0); }
public static int ToInteger (cet objet strValue) { return strValue.ToInteger (0); }
public static bool ToBool (cet objet strValue) { return strValue.ToBool (false); }
objet statique public InvokeMethod (cet objet obj, chaîne methodName, params object [] paramètres) {
return InvokeMethod<object>(obj, methodName, paramètres);
}
public static T InvokeMethod<T>(cet objet obj, string methodName) {
return InvokeMethod<T>(obj, methodName, null);
}
public static T InvokeMethod<T>(cet objet obj, string methodName, params object[] paramètres) {
var type = obj.GetType();
var méthode = type.GetMethod(methodName);
if(method == null) throw new ArgumentException(string.Format("Méthode '{0}' introuvable.", methodName), methodName);
var valeur = méthode.Invoke(obj, paramètres);
return (la valeur est T ? (T) valeur : default(T));
}
objet statique public GetPropertyValue (cet objet obj, chaîne propertyName) {
return GetPropertyValue<object>(obj, propertyName, null);
}
public static T GetPropertyValue<T>(cet objet obj, chaîne propertyName) {
return GetPropertyValue<T>(obj, propertyName, default(T));
}
public static T GetPropertyValue<T>(cet objet 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 valeur = propriété.GetValue(obj, null);
return (la valeur est T ? (T) valeur : defaultValue);
}
public static void SetPropertyValue (cet objet obj, chaîne propertyName, valeur de l'objet) {
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, valeur, null);
}
public static T GetAttribute<T>(cet objet obj) où T : Attribut {
return GetAttribute<T>(obj, true);
}
public static T GetAttribute<T>(cet objet obj, bool includeInherited) où T : Attribut {
var type = (obj comme Type ?? obj.GetType());
var attributs = type.GetCustomAttributes(typeof(T), includeInherited);
if((attributs != null) && (attributs.Length > 0)) {
return (attributs[0] comme T);
}
renvoie null ;
}
public static IEnumerable<T> GetAttributes<T>(cet objet obj) où T : Attribut {
return GetAttributes<T>(obj);
}
public static IEnumerable<T> GetAttributes<T>(cet objet obj, bool includeInherited) où T : Attribut {
var type = (obj comme Type ?? obj.GetType());
foreach (attribut var dans type.GetCustomAttributes (typeof (T), includeInherited)) {
if (attribut is T) rendement rendement (T) attribut ;
}
}
public static bool IsType (cet objet obj, Type type) {
return obj.GetType().Equals(type);
}
public static T ToType<T>(cette valeur d'objet) { return (T)value; }
public static bool IsArray (cet objet obj) {
return obj.IsType(typeof(System.Array));
}
public static bool IsDBNull (cet objet obj) {
return obj.IsType(typeof(DBNull));
}
public static byte[] Serialize(cette valeur d'objet) {
MemoryStream ms = nouveau MemoryStream();
BinaryFormatter bf1 = new BinaryFormatter();
bf1.Serialize(ms, valeur);
retourner ms.ToArray();
}
public static void CheckOnNull (cet objet @this, chaîne paramètreName) {
if (@this.IsNull()) lance une nouvelle ArgumentNullException(parameterName);
}
public static void CheckOnNull (cet objet @this, nom du paramètre de chaîne, message de chaîne) {
if (@this.IsNull()) lance une nouvelle ArgumentNullException (parameterName, message);
}
public static bool IsNull (cet objet @this) {
return @this == null;
}
public static bool IsNotNull (cet objet @this) {
return [email protected] ();
}
public static T UnsafeCast<T>(cette valeur d'objet) {
valeur de retour.IsNull() ? par défaut(T) : (T)valeur ;
}
public static T SafeCast<T>(cette valeur d'objet) {
la valeur de retour est T ? value.UnsafeCast<T>() : par défaut(T);
}
public static bool InstanceOf<T>(cette valeur d'objet) {
la valeur de retour est T ;
}
public static void SerializeXmlFile (cet objet o, chaîne fileName) {
Sérialiseur XmlSerializer = new XmlSerializer(o.GetType());
if (!FileFolder.FileExists(fileName)) return;
en utilisant (FileStream stream = new FileStream(fileName, FileMode.Create, Fileaccess.Write)) serializer.Serialize(stream, o);
}
public static T DeserializeXmlFile<T>(string fileName) {
À;
Sérialiseur XmlSerializer = new XmlSerializer(typeof(T));
en utilisant (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) o = (T)serializer.Deserialize(stream);
revenir o;
}
chaîne statique publique SerializeXml (cet objet o) {
Sérialiseur XmlSerializer = new XmlSerializer(o.GetType());
StringBuilder stringBuilder = new StringBuilder();
en utilisant (TextWriter textWriter = new StringWriter(stringBuilder)) serializer.Serialize(textWriter, o);
return stringBuilder.ToString();
}
public static T DeserializeXml<T>(cette chaîne xml) {
return (T)Désérialiser(xml, typeof(T));
}
objet statique public Désérialiser (chaîne xml, type type) {
objet o;
Sérialiseur XmlSerializer = new XmlSerializer(type);
using (TextReader textReader = new StringReader(xml)) o = serializer.Deserialize(textReader);
revenir o;
}
public static void Write (cet objet o) { Msg.Write (o); }
public static void WriteEnd (cet objet o) { Msg.WriteEnd (o); }
}
}