배열이/객체의 목록은 같은 이름이지만 다른 유형을 가질 때이 같은 문제가 발생 된 감사 등 INT, 문자열, 같은-같은 이름의 간단한 형식을 복사합니다 즉, ORMAnimals [] 유형의 Animals 속성은 Animals []) 유형의 Animals 속성에 매핑됩니다.
public class CloneInjection : ConventionInjection
{
protected override bool Match(ConventionInfo c)
{
return c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Value != null;
}
protected override object SetValue(ConventionInfo c)
{
//for value types and string just return the value as is
if (c.SourceProp.Type.IsValueType || c.SourceProp.Type == typeof(string)
|| c.TargetProp.Type.IsValueType || c.TargetProp.Type == typeof(string))
return c.SourceProp.Value;
//handle arrays
if (c.SourceProp.Type.IsArray)
{
var arr = c.SourceProp.Value as Array;
var clone = Activator.CreateInstance(c.TargetProp.Type, arr.Length) as Array;
for (int index = 0; index < arr.Length; index++)
{
var a = arr.GetValue(index);
if (a.GetType().IsValueType || a.GetType() == typeof(string)) continue;
clone.SetValue(Activator.CreateInstance(c.TargetProp.Type.GetElementType()).InjectFrom<CloneInjection>(a), index);
}
return clone;
}
if (c.SourceProp.Type.IsGenericType)
{
//handle IEnumerable<> also ICollection<> IList<> List<>
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 tlist = typeof(List<>).MakeGenericType(t);
var list = Activator.CreateInstance(tlist);
var addMethod = tlist.GetMethod("Add");
foreach (var o in c.SourceProp.Value as IEnumerable)
{
var e = Activator.CreateInstance(t).InjectFrom<CloneInjection>(o);
addMethod.Invoke(list, new[] { e }); // in 4.0 you can use dynamic and just do list.Add(e);
}
return list;
}
//unhandled generic type, you could also return null or throw
return c.SourceProp.Value;
}
//for simple object types create a new instace and apply the clone injection on it
return Activator.CreateInstance(c.TargetProp.Type)
.InjectFrom<CloneInjection>(c.SourceProp.Value);
}
}
당신이 ValueInjecter의 CodePlex의 페이지에서 깊은 복제 페이지를 보았다 : 깊은 복제 page에 척 노리스가 가지고있는 샘플 코드에 약간의 비틀기와
나는 내 테스트 코드에서 작업을 있어요? http://valueinjecter.codeplex.com/wikipage?title=Deep%20Cloning&referringTitle=Home – Omu
헤이 척. 그래, 내가 했어. 그것은 내 [nhibernate] 자식 엔티티를 깊게 복제하지 않았습니다. – user52212
일부 코드를 게시하면 기본 InjectFrom()이 동일한 이름과 같은 유형의 심볼을 삽입하므로 다른 멤버에 영향을주지는 않습니다. 다른 유형 (더 이상 복제하지 않는 유형에서 수행하는 경우 DeepClone이 작동하지 않는 이유입니다) – Omu