Verwenden des Systems;
mit System.Collections.Generic;
mit System.Linq;
Verwenden von System.Text;
Verwenden von System.Reflection;
Verwenden von System.Data;
mit System.Data.Common;
mit System.Web.Script.Serialization;
mit System.IO;
Verwendung von System.Security.Cryptography;
mit System.ComponentModel;
mit System.Runtime.Serialization.Formatters.Binary;
mit System.xml.Serialization;
Namespace Pub.Class {
öffentliche statische Klasse ObjectExtensions {
öffentliche statische Zeichenfolge ToJson(this object obj){
return ToJson(obj, null);
}
öffentliche statische Zeichenfolge 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-Konverter = TypeDescriptor.GetConverter(value);
if(converter != null) {
if(converter.CanConvertTo(targetType)) return (T) converter.ConvertTo(value, targetType);
}
konverter = TypeDescriptor.GetConverter(targetType);
if(converter != null) {
Versuchen Sie es mit { if(converter.CanConvertFrom(value.GetType())) return (T) converter.ConvertFrom(value); } fangen {}
}
}
return defaultValue;
}
public static T ConvertTo<T>(this object value, T defaultValue, boolignoreException) {
if(ignoreException) {
versuchen {
Rückgabewert.ConvertTo<T>();
}
fangen {
return defaultValue;
}
}
Rückgabewert.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; }
öffentliches statisches 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; }
öffentliche statische Dezimalzahl 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; }
öffentliche statische Dezimalzahl 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 (Ausdruck != 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); }
öffentliches statisches Byte ToTinyInt(this object strValue) { return strValue.ToTinyInt(0); }
public static short ToSmallInt(this object strValue) { return strValue.ToSmallInt(0); }
öffentliche statische Dezimalzahl 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); }
öffentliche statische Dezimalzahl 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); }
öffentliches statisches Objekt InvokeMethod(dieses Objekt obj, string methodName, params object[] Parameter) {
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>(dieses Objekt obj, string methodName, params object[] Parameter) {
var type = obj.GetType();
var method = type.GetMethod(methodName);
if(method == null) throw new ArgumentException(string.Format("Methode '{0}' nicht gefunden.", methodName), methodName);
var value = method.Invoke(obj, Parameter);
return (Wert ist T ? (T) Wert : default(T));
}
öffentliches statisches Objekt 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 (Wert ist T? (T) Wert: Standardwert);
}
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);
}
null zurückgeben;
}
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(Attribut ist T) yield return (T)-Attribut;
}
}
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));
}
öffentliches statisches 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 [email protected] ();
}
public static T UnsafeCast<T>(dieser Objektwert) {
Rückgabewert.IsNull() ? default(T) : (T)Wert;
}
public static T SafeCast<T>(dieser Objektwert) {
Rückgabewert ist T ? value.UnsafeCast<T>() : default(T);
}
public static bool InstanceOf<T>(dieser Objektwert) {
Rückgabewert ist 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) {
Zu;
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) o = (T)serializer.Deserialize(stream);
Rückkehr o;
}
öffentliche statische Zeichenfolge 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));
}
öffentliches statisches Objekt Deserialize(string xml, Type type) {
Objekt o;
XmlSerializer serializer = new XmlSerializer(type);
using (TextReader textReader = new StringReader(xml)) o = serializer.Deserialize(textReader);
Rückkehr o;
}
public static void Write(this object o) { Msg.Write(o); }
public static void WriteEnd(this object o) { Msg.WriteEnd(o); }
}
}