Collections 使用ValueInjector映射类的步骤

Collections 使用ValueInjector映射类的步骤,collections,mapping,valueinjecter,Collections,Mapping,Valueinjecter,快速解决问题以下代码不会出现映射。有人能解释一下原因吗?或者我应该做什么来实现映射 var parent=new parent(); Add(new ChildOne(){Name=“ChildOne”}); Add(new ChildTwo(){Name=“ChildTwo”}); AnotherParent AnotherParent=新的AnotherParent(); 另一个父项。从(父项)注入; 必修课如下 另一个孩子 public类另一个childone { 公共字符串名称{get

快速解决问题以下代码不会出现映射。有人能解释一下原因吗?或者我应该做什么来实现映射

var parent=new parent();
Add(new ChildOne(){Name=“ChildOne”});
Add(new ChildTwo(){Name=“ChildTwo”});
AnotherParent AnotherParent=新的AnotherParent();
另一个父项。从(父项)注入;
必修课如下

另一个孩子
public类另一个childone
{
公共字符串名称{get;set;}
}
另一个孩子2岁
公共类另一个孩子2
{
公共字符串名称{get;set;}
}
另一位家长
公共类另一个父类
{
公共ICollection ChildOne{get;set;}
公共ICollection ChildTwo{get;set;}
公开另一个父项()
{
ChildOne=新集合();
ChildTwo=新集合();
}
}
孩子二
公共类ChildTwo
{
公共字符串名称{get;set;}
}
孩子一
公共类ChildOne
{
公共字符串名称{get;set;}
}
父母亲
公共类父类
{
公共ICollection ChildOne{get;set;}
公共ICollection ChildTwo{get;set;}
公共家长()
{
ChildOne=新集合();
ChildTwo=新集合();
}
}

我相信默认情况下,值注入器只会注入具有相同类型的相同名称的属性。您可以使用以下代码所述的值注入器文档中的
CloneInjection
示例来解决此问题:

公共类克隆注射:约定注射
{
受保护的覆盖布尔匹配(ConventionInfo c)
{
返回c.SourceProp.Name==c.TargetProp.Name&&c.SourceProp.Value!=null;
}
受保护的覆盖对象设置值(ConventionInfo c)
{
//对于值类型和字符串,只需按原样返回值
if(c.SourceProp.Type.IsValueType | | c.SourceProp.Type==typeof(字符串)
||c.TargetProp.Type.IsValueType | | c.TargetProp.Type==typeof(字符串))
返回c.SourceProp.Value;
//句柄数组
if(c.SourceProp.Type.IsArray)
{
var arr=c.SourceProp.Value作为数组;
var clone=Activator.CreateInstance(c.TargetProp.Type,arr.Length)作为数组;
对于(int index=0;index
如果包括上述
克隆注射
代码,则需要执行以下操作:

anotherParent.InjectFrom(parent);
而不是:

anotherParent.InjectFrom(parent);

我相信默认情况下,值注入器只会注入具有相同类型的相同名称的属性。您可以使用以下代码所述的值注入器文档中的
CloneInjection
示例来解决此问题:

公共类克隆注射:约定注射
{
受保护的覆盖布尔匹配(ConventionInfo c)
{
返回c.SourceProp.Name==c.TargetProp.Name&&c.SourceProp.Value!=null;
}
受保护的覆盖对象设置值(ConventionInfo c)
{
//对于值类型和字符串,只需按原样返回值
if(c.SourceProp.Type.IsValueType | | c.SourceProp.Type==typeof(字符串)
||c.TargetProp.Type.IsValueType | | c.TargetProp.Type==typeof(字符串))
返回c.SourceProp.Value;
//句柄数组
if(c.SourceProp.Type.IsArray)
{
var arr=c.SourceProp.Value作为数组;
var clone=Activator.CreateInstance(c.TargetProp.Type,arr.Length)作为数组;
对于(int index=0;index