我正在尝试将具有相同布局的类映射到我要映射到的类。一切顺利,除非我尝试映射Object集合。例如,当我尝试映射源类中定义的此属性时:
[System.Xml.Serialization.XmlElementAttribute("trust", typeof(Trust))]
[System.Xml.Serialization.XmlElementAttribute("valuation", typeof(Valuation))]
[System.Xml.Serialization.XmlElementAttribute("waiver_of_premium_ind", typeof(YesNo))]
[System.Xml.Serialization.XmlElementAttribute("written_under_trust_ind", typeof(YesNo), IsNullable = true)]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public object[] Items
{
get { return this.itemsField; }
set { this.itemsField = value; }
}
我发现它没有映射,但保留在与源对象相同的命名空间中,即使它是目标对象中的集合。
我想知道你对此事有什么看法吗?
编辑: 通过示例的更多信息 - 源类:
namespace Namespace1
{
public class Person
{
public int PersonID { get; set; }
public List<Arm> Arms { get; set; }
[System.Xml.Serialization.XmlElementAttribute("_arms", typeof(Arm))]
[System.Xml.Serialization.XmlElementAttribute("_hand", typeof(Hand))]
public object[] Items { get; set; }
}
public class Arm
{
public Hand Hand { get; set; }
}
public class Hand
{
public int HandID { get; set; }
public string HandSide { get; set; }
public List<Fingers> Fingers { get; set; }
}
public class Fingers
{
public int FingerNumber { get; set; }
}
}
目的地类:
namespace Namespace2
{
public class Person
{
public int PersonID { get; set; }
public List<Arm> Arms { get; set; }
[System.Xml.Serialization.XmlElementAttribute("_arms", typeof(Arm))]
[System.Xml.Serialization.XmlElementAttribute("_hand", typeof(Hand))]
public object[] Items { get; set; }
}
public class Arm
{
public Hand Hand { get; set; }
}
public class Hand
{
public int HandID { get; set; }
public string HandSide { get; set; }
public List<Fingers> Fingers { get; set; }
}
public class Fingers
{
public int FingerNumber { get; set; }
}
}
用于映射两个名称空间中的两种类型和所有嵌套类型的代码:
public static void CreateMappings(string nsFrom, string nsTo, Type typeFrom)
{
Assembly assembly = Assembly.GetAssembly(typeFrom);
var TypesInNamespace = assembly.GetTypes().Where(type => type.Namespace == nsFrom);
foreach (var sourceType in TypesInNamespace)
{
Type destinationType = Type.GetType(sourceType.FullName.Replace(nsFrom, nsTo));
Mapper.CreateMap(sourceType, destinationType);
}
}
然后我从Namespace1创建我的person对象并使用上面的函数创建映射,如下所示:
CreateMappings("Namespace1", "Namespace2", typeof(Namespace1.Person));
之后我调用map函数:
var result = Mapper.Map<Namespace2.Person>(person);
这为Items对象数组映射了Person类的所有属性,只是精细的EXCEPT。它会传输对象,但它们仍然属于Namespace1而不是Namespace2命名空间。
可以在观察窗口中找到问题的图像here
如果您喜欢here
,可以下载控制台应用感谢您提供任何帮助。 中号
答案 0 :(得分:0)
我尝试过以下操作,似乎有效;
public class Tester
{
public void Test()
{
AutoMapper.Mapper.CreateMap<FirstObject, SecondObject>();
FirstObject t = new FirstObject();
t.Items = new object[] { new Item() { Id = 1 }, new Item() { Id = 2 } };
SecondObject result = AutoMapper.Mapper.Map<SecondObject>(t);
}
}
public class FirstObject
{
public object[] Items { get; set; }
}
public class SecondObject
{
public object[] Items { get; set; }
}
public class Item
{
public int Id { get; set; }
}
答案 1 :(得分:0)
我知道这确实很老,但是我确实找到了答案(至少对于最新版本的AutoMapper(在此答案时为8.0)。您需要为执行映射的Person编写自定义android:layout_gravity="center"
对象[]。
在我的情况下,我有一个像这样的对象(删除了xml属性):
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:paddingBottom="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/b1"
style="@style/Widget.AppCompat.Button"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="2dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#f6f3e0"
android:drawableLeft="@drawable/capitole"
android:foreground="?attr/selectableItemBackground"
android:padding-left
android:text="Button1"
android:textAppearance="@style/TextAppearance.AppCompat.Button"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/b2"
style="@style/Widget.AppCompat.Button"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="2dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#cbe8eb"
android:drawableLeft="@drawable/random"
android:foreground="?attr/selectableItemBackground"
android:padding-left
android:text="Random"
android:textAppearance="@style/TextAppearance.AppCompat.Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/b3"
style="@style/Widget.AppCompat.Button"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="2dp"
android:background="@android:color/holo_orange_light"
android:drawableLeft="@drawable/developer"
android:foreground="?attr/selectableItemBackground"
android:paddingDown="45dp"
android:text="Developer"
android:textAppearance="@style/TextAppearance.AppCompat.Button" />
</LinearLayout>
</LinearLayout>
自定义类型转换器看起来像这样:
ITypeConverter