using System.Collections;
public static void Main() {
Bar bar = new Bar("BarVal");
Foo foo = new Foo("FooVal", bar);
Bar bar_t = new Bar("non");
Foo foo_t = new Foo("non", bar_t);
IFoo.PassObjectValues<Foo>(foo, foo_t);
Console.WriteLine("\n----- Object Properties -----");
Console.WriteLine("Source");
IFoo.PrintProperties(foo);
Console.WriteLine("Target");
IFoo.PrintProperties(foo_t);
public class Bar : IFoo {
public string _value { get; set; }
public int[] _array { get; set; }
public class Foo : IFoo {
public string _valueTypeProp { get; set; }
public Bar _referenceTypeProp { get; set; }
public Foo(string valueType, Bar propType) {
_valueTypeProp = valueType;
_referenceTypeProp = propType;
public abstract class IFoo {
public static void PrintProperties(object obj) {
public static void PrintProperties(object obj, int indent) {
string indentString = new string(' ', indent);
Type objType = obj.GetType();
PropertyInfo[] properties = objType.GetProperties();
foreach (PropertyInfo property in properties) {
object propValue = property.GetValue(obj, null);
var elems = propValue as IList;
foreach (var item in elems) {
PrintProperties(item, indent + 3);
if (property.PropertyType.Assembly == objType.Assembly) {
Console.WriteLine("{0}{1} | {2}:", indentString, property.GetValue(obj).GetHashCode(), property.Name);
PrintProperties(propValue, indent + 2);
Console.WriteLine("{0}{1} | {2}: {3}", indentString, property.GetValue(obj).GetHashCode(),property.Name, propValue);
public static void PassObjectValues<T>(object source, object target) {
DoValuePassing(source, target);
private static void DoValuePassing(object source, object target) {
Type type = source.GetType();
foreach (var prop in type.GetProperties()) {
var sourceObj = prop.GetValue(source);
var targetObj = prop.GetValue(target);
var elems = sourceObj as IList;
foreach (var item in elems) {
DoValuePassing(sourceObj, targetObj);
if (prop.PropertyType.IsSubclassOf(typeof(IFoo))) {
DoValuePassing(sourceObj, targetObj);
Console.WriteLine("-----------------------"+ prop.Name +"----------------------------");
Console.WriteLine("Source ID: " + sourceObj.GetHashCode() + " | Value: " + sourceObj);
Console.WriteLine("Target ID: " + targetObj.GetHashCode() + " | Value: " + targetObj);
prop.SetValue(target, sourceObj);
sourceObj = prop.GetValue(source);
targetObj = prop.GetValue(target);
Console.WriteLine("Source ID: " + sourceObj.GetHashCode() + " | Value: " + sourceObj);
Console.WriteLine("Target ID: " + targetObj.GetHashCode() + " | Value: " + targetObj);
Console.WriteLine("-----------------------------------------------------------");
foreach (var field in type.GetFields()) {
var sourceObj = field.GetValue(source);
var targetObj = field.GetValue(target);
var elems = sourceObj as IList;
foreach (var item in elems) {
DoValuePassing(sourceObj, targetObj);
if(field.FieldType.IsSubclassOf(typeof(IFoo))) {
DoValuePassing(sourceObj, targetObj);
field.SetValue(target, sourceObj);