using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Threading.Tasks; /// /// /// A collection of extension methods. /// internal static class ObjectExtensions { /// /// Tries to copy the specified source object properties to the target object properties. /// /// The source object. /// The target object. internal static void MapTo(this object sourceObject,object targetObject) { foreach (PropertyInfo prop in sourceObject.GetType().GetProperties()) { PropertyInfo prop2 = sourceObject.GetType().GetProperty(prop.Name); prop2.SetValue(targetObject, prop.GetValue(sourceObject, null), null); } } /// /// Returns the name of the specified property. /// /// /// The type of the t. /// The object. /// The property accessor. /// internal static String NameOf(this T obj, Expression> propertyAccessor) { if (propertyAccessor.Body.NodeType == ExpressionType.MemberAccess) { var memberExpression = propertyAccessor.Body as MemberExpression; if (memberExpression == null) return null; return memberExpression.Member.Name; } return null; } internal static string ToStringReflection(this object obj) { StringBuilder sb = new StringBuilder(); foreach (System.Reflection.PropertyInfo property in obj.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly)) { sb.Append(property.Name); sb.Append(": "); if (property.GetIndexParameters().Length > 0) { sb.Append("Indexed Property cannot be used"); } else { sb.Append(property.GetValue(obj, null)); } sb.Append(System.Environment.NewLine); } return sb.ToString(); } /// /// Extension for 'Object' that copies the properties to a destination object. /// /// The source. /// The destination. internal static void CopyProperties(this object source, object destination) { // If any this null throw an exception if (source == null || destination == null) throw new Exception("Source or/and Destination Objects are null"); // Getting the Types of the objects Type typeDest = destination.GetType(); Type typeSrc = source.GetType(); // Iterate the Properties of the source instance and // populate them from their desination counterparts PropertyInfo[] srcProps = typeSrc.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly); foreach (PropertyInfo srcProp in srcProps) { //if (!srcProp.CanRead) //{ // continue; //} //PropertyInfo targetProperty = typeDest.GetProperty(srcProp.Name); //if (targetProperty == null) //{ // continue; //} //if (!targetProperty.CanWrite) //{ // continue; //} //if (targetProperty.GetSetMethod(true) != null && targetProperty.GetSetMethod(true).IsPrivate) //{ // continue; //} //if ((targetProperty.GetSetMethod().Attributes & MethodAttributes.Static) != 0) //{ // continue; //} //if (!targetProperty.PropertyType.IsAssignableFrom(srcProp.PropertyType)) //{ // continue; //} // Passed all tests, lets set the value srcProp.SetValue(destination, srcProp.GetValue(source, null), null); } } }