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;
publicstaticobject InvokeMethod(thisobject obj, string methodName, paramsobject[] parameters) { return InvokeMethod<object>(obj, methodName, parameters); } publicstatic T InvokeMethod<T>(thisobject obj, string methodName) { return InvokeMethod<T>(obj, methodName, null); } publicstatic T InvokeMethod<T>(thisobject obj, string methodName, paramsobject[] parameters) { var type = obj.GetType(); var method = type.GetMethod(methodName);
if(method ==null) thrownew ArgumentException(string.Format("Method '{0}' not found.", methodName), methodName);
var value = method.Invoke(obj, parameters); return (value is T ? (T) value : default(T)); }
publicstaticobject GetPropertyValue(thisobject obj, string propertyName) { return GetPropertyValue<object>(obj, propertyName, null); } publicstatic T GetPropertyValue<T>(thisobject obj, string propertyName) { return GetPropertyValue<T>(obj, propertyName, default(T)); } publicstatic T GetPropertyValue<T>(thisobject obj, string propertyName, T defaultValue) { var type = obj.GetType(); var property = type.GetProperty(propertyName);
if(property ==null) thrownew ArgumentException(string.Format("Property '{0}' not found.", propertyName), propertyName);
var value = property.GetValue(obj, null); return (value is T ? (T) value : defaultValue); } publicstaticvoid SetPropertyValue(thisobject obj, string propertyName, object value) { var type = obj.GetType(); var property = type.GetProperty(propertyName);
if(property ==null) thrownew ArgumentException(string.Format("Property '{0}' not found.", propertyName), propertyName);
property.SetValue(obj, value, null); }
publicstatic T GetAttribute<T>(thisobject obj) where T : Attribute { return GetAttribute<T>(obj, true); } publicstatic T GetAttribute<T>(thisobject 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); } returnnull; }
publicstatic IEnumerable<T> GetAttributes<T>(thisobject obj) where T : Attribute { return GetAttributes<T>(obj); } publicstatic IEnumerable<T> GetAttributes<T>(thisobject 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) yieldreturn (T) attribute; } }