using System.Collections;
using Omu.ValueInjecter.Injections;
public static void Main()
Console.WriteLine("Hello World");
testv.InjectFrom<CloneInjection>(test);
Console.WriteLine(testv.s1);
Console.WriteLine(testv.s2);
Console.WriteLine(testv.c.s1);
Console.WriteLine(testv.c.s1);
public string s1 { get; set; }
public string s2 { get; set; }
public C2 c { get; set; }
public string s1 { get; set; }
public string s2 { get; set; }
public string s1 { get; set; }
public string s2 { get; set; }
public C2v c { get; set; }
public string s1 { get; set; }
public string s2 { get; set; }
public class CloneInjection : Omu.ValueInjecter.Injections.LoopInjection
protected override void Execute(PropertyInfo sp, object source, object target)
var tp = target.GetType().GetProperty(sp.Name);
var val = sp.GetValue(source);
tp.SetValue(target, GetClone(sp, tp, val));
private static object GetClone(PropertyInfo sp, PropertyInfo tp, object val)
if (sp.PropertyType.IsValueType || sp.PropertyType == typeof(string))
if (sp.PropertyType.IsArray)
var arrClone = arr.Clone() as Array;
for (int index = 0; index < arr.Length; index++)
var a = arr.GetValue(index);
if (a.GetType().IsValueType || a is string) continue;
arrClone.SetValue(Activator.CreateInstance(a.GetType()).InjectFrom<CloneInjection>(a), index);
if (sp.PropertyType.IsGenericType)
if (sp.PropertyType.GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable)))
var genericType = tp.PropertyType.GetGenericArguments()[0];
var listType = typeof(System.Collections.Generic.List<>).MakeGenericType(genericType);
var list = Activator.CreateInstance(listType);
var addMethod = listType.GetMethod("Add");
foreach (var o in val as IEnumerable)
var listItem = genericType.IsValueType || genericType == typeof(string) ? o : Activator.CreateInstance(genericType).InjectFrom<CloneInjection>(o);
addMethod.Invoke(list, new[] { listItem });
return Activator.CreateInstance(tp.PropertyType)
.InjectFrom<CloneInjection>(val);