I created my own injection class for ValueInjecter , which performs recursive injection for shared child collections, but works with existing targets and not cloning, like the "CloneInjection" sample on site VI. However, I am currently using a well-known type (ICollection <MyTargetType>), so the injection class is suitable for only one hard-coded type. It seems I can’t imagine a way to dynamically generalize, any suggestions? Here is the SetValue code for my "RecursiveInjection" class.
if (c.SourceProp.Type.IsValueType || c.SourceProp.Type == typeof(string))
return c.SourceProp.Value;
if (c.SourceProp.Type.IsGenericType)
{
if (c.SourceProp.Type.GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable)))
{
var t = c.TargetProp.Type.GetGenericArguments()[0];
if (t.IsValueType || t == typeof(string)) return c.SourceProp.Value;
var targetCollection = c.TargetProp.Value as ICollection<MyTargetType>;
foreach (var o in c.SourceProp.Value as IEnumerable)
{
var sourceID = (int)o.GetProps().GetByName("ID").GetValue(o);
var target = targetCollection.SingleOrDefault(x => x.ID == sourceID);
if (target != null)
{
target.InjectFrom<RecursiveInjection>(o);
}
}
return targetCollection;
}
}
Thanks DanO
Dano source
share