SomeObjectA
sourceObjRef = new SomeObjectA();SomeObjectB targetObjRef = new SomeObjectB();
new ReflectionSynchronizer().Sync(sourceObjRef , targetObjRef );IDictionary sourceDictionary = new Hashtable();
SomeObject targetObjRef = new SomeObject();
new ReflectionSynchronizer().Sync(sourceDictionary , targetObjRef );here is the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
namespace Helpers
{
public class ReflectionSynchronizer
{
/// <summary>
/// synchronizes a Dictionary to an objects properties
/// uses reflection to figure out types to convert objects in entry.Value to when setting object’s properties
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
public void Sync(IDictionary source, object target)
{
foreach (DictionaryEntry entry in source)
{
try
{
PropertyInfo targetObjectProperty = target.GetType().GetProperty(entry.Key.ToString());
if (targetObjectProperty != null)
{
object sourceObjectValue = entry.Value;
if (sourceObjectValue != null)
{
//does handle nullable types – see overload for known in advanced
object valueToAssign = null;
To(sourceObjectValue, out valueToAssign, sourceObjectValue.GetType());
if (valueToAssign != null)
{
targetObjectProperty.SetValue(target, valueToAssign, null);
}
}
}
}
catch (ApplicationException ex)
{
Debug.WriteLine(ex.Message);
}
}
}
/// <summary>
/// synchronizes an object reference’s properties’ values to another object reference’s properties’ values
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
public void Sync(object source, object target)
{
foreach (PropertyInfo sourceObjectProperty in source.GetType().GetProperties())
{
try
{
PropertyInfo targetObjectProperty = target.GetType().GetProperty(sourceObjectProperty.Name);
if (targetObjectProperty != null && targetObjectProperty.PropertyType.Equals(sourceObjectProperty.PropertyType))
{
object sourceObjectValue = sourceObjectProperty.GetValue(source, null);
if (sourceObjectValue != null)
{
//does handle nullable types – see overload for known in advanced
object valueToAssign = null;
To(sourceObjectValue, out valueToAssign, sourceObjectValue.GetType());
if (valueToAssign != null)
{
targetObjectProperty.SetValue(target, valueToAssign, null);
}
}
}
}
catch (ApplicationException ex)
{
Debug.WriteLine(ex.Message);
}
}
}
/// <summary>
/// copies a value to another
/// </summary>
/// <param name="srcValue"></param>
/// <param name="targetValue"></param>
/// <param name="t"></param>
public void To(object srcValue, out object targetValue, Type t)
{
targetValue = null;
if (srcValue == DBNull.Value) return;
if (IsNullable(t))
{
if (srcValue == null)
{
return;
}
targetValue = UnderlyingTypeOf(t);
}
targetValue = Convert.ChangeType(srcValue, t);
}
/// <summary>
/// generic version of To
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public T To<T>(object value, T defaultValue)
{
if (value == DBNull.Value) return defaultValue;
Type t = typeof(T);
if (IsNullable(t))
{
if (value == null) return default(T);
t = UnderlyingTypeOf(t);
}
return (T)Convert.ChangeType(value, t);
}
/// <summary>
/// figures out if the Type is Nullable"/>
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
private bool IsNullable(Type t)
{
if (!t.IsGenericType) return false;
Type g = t.GetGenericTypeDefinition();
return (g.Equals(typeof(Nullable<>)));
}
/// <summary>
/// gets the underlying Type
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
private Type UnderlyingTypeOf(Type t)
{
return t.GetGenericArguments()[0];
}
}
}