Skip to content

Instantly share code, notes, and snippets.

@janosorcsik
Last active December 11, 2017 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janosorcsik/192d386da16680964279664bdca97fed to your computer and use it in GitHub Desktop.
Save janosorcsik/192d386da16680964279664bdca97fed to your computer and use it in GitHub Desktop.
Object mapper
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace ObjectExtensions
{
public static class Mapper
{
public static T SetProperty<T>(this T host, string propertyName, object value, bool hideErrors = false)
{
PropertyInfo pi = host.GetType().GetProperty(propertyName);
if (pi == null)
{
if (!hideErrors)
throw new ArgumentException("Property not found", propertyName);
}
else
try
{
pi.SetValue(host, value);
}
catch
{
if (!hideErrors)
throw;
}
return host;
}
public static bool CopyPublicInstancePropsAndFields(object source, object target)
{
if (source == null || target == null)
return false;
bool retVal = true;
// create dict of target fields
FieldInfo[] targetFIArr = target.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
SortedDictionary<string, FieldInfo> targetFIDict = new SortedDictionary<string, FieldInfo>();
foreach (FieldInfo fi in targetFIArr)
targetFIDict[fi.Name] = fi;
// create dict of target props
PropertyInfo[] targetPIArr = target.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
SortedDictionary<string, PropertyInfo> targetPIDict = new SortedDictionary<string, PropertyInfo>();
foreach (PropertyInfo pi in targetPIArr)
targetPIDict[pi.Name] = pi;
// copy fields of same name, try target property if no field found
FieldInfo[] sourceFIArr = source.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo fi in sourceFIArr)
if (targetFIDict.ContainsKey(fi.Name))
try
{
targetFIDict[fi.Name].SetValue(target, fi.GetValue(source));
}
catch
{
retVal = false;
}
else if (targetPIDict.ContainsKey(fi.Name))
try
{
var pi2 = targetPIDict[fi.Name];
if (pi2.CanWrite)
pi2.SetValue(target, fi.GetValue(source), null);
}
catch
{
retVal = false;
}
// copy properties of same name, try target field if no property found
PropertyInfo[] sourcePIArr = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo pi in sourcePIArr)
if (pi.CanRead)
{
if (targetPIDict.ContainsKey(pi.Name))
try
{
var pi2 = targetPIDict[pi.Name];
if (pi2.CanWrite)
{
if ((pi2.PropertyType.IsGenericType && pi2.PropertyType.IsClass && pi2.PropertyType.GetGenericArguments()[0].BaseType.BaseType == typeof(object)) ||
(pi2.PropertyType.IsGenericType && pi2.PropertyType.IsInterface && pi2.PropertyType.GetGenericArguments()[0].BaseType == typeof(object)))
{
CopyPublicInstancePropsAndFields(pi.GetValue(source, null),
pi2.GetValue(target, null));
}
else
{
pi2.SetValue(target, pi.GetValue(source, null), null);
}
}
}
catch
{
retVal = false;
}
else if (targetFIDict.ContainsKey(pi.Name))
try
{
targetFIDict[pi.Name].SetValue(target, pi.GetValue(source, null));
}
catch
{
retVal = false;
}
}
return retVal;
}
public static string ModelPropertiesToLogString(object model)
{
StringBuilder strBuilder = new StringBuilder();
PropertyInfo[] properties = model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo p in properties)
{
object val;
try
{
val = p.GetValue(model);
}
catch
{
val = "!!ERROR!!";
}
if (val is string)
strBuilder.AppendFormat("{0}='{1}';", p.Name, val);
else
strBuilder.AppendFormat("{0}={1};", p.Name, val);
}
return strBuilder.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment