亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

自動映射數組類

自動映射數組類

C#
qq_遁去的一_1 2023-09-24 16:23:25
我有一個目標對象ArrayOfStudents[]含有StudentId,    AddressInfo,    MarksInfo源對象是public class Details{    public Student[] Student;)學生類包含學生號、地址信息、標記信息我想要地圖Student[]和ArrayOfStudents[]我嘗試了以下方法,但沒有成功Map.CreateMap<Student,ArrayOfStudents>() .ReverseMap(); Map.CreateMap<Details.Student,ArrayOfStudents>() .ReverseMap();我應該如何繪制這個案例?它拋出以下未映射錯誤學生號、地址信息、標記信息
查看完整描述

1 回答

?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

使用 Automapper,您可以將一種類型映射到另一種類型。當您這樣做時,將自動映射相同類型的數組。


ArrayOfStudents在您的情況下,您將創建和之間的地圖Student。這將是一個簡單的映射,因為兩個映射類型之間的類型和名稱是相同的:


public class MappingProfile : Profile

{

    public MappingProfile()

    {

        this.CreateMap<Student, ArrayOfStudents>();


        this.CreateMap<ArrayOfStudents, Student>();

    }

}

現在,無論您打算在哪里進行實際映射(例如 RESTful 控制器),您都可以執行以下操作:


public class MyController

{

    private readonly IMapper mapper;


    public MyController(IMapper mapper)

    {

        this.mapper = mapper;

    }


    // Then in any of your methods:

    [HttpGet]

    public IActionResult MyMethod()

    {

        var objectsToMap = details.Student; // This is an array of Student type.

        var mappedObjects = this.mapper.Map(objectsToMap); // This will be an array of ArrayOfStudents.

        // do what you will with the mapped objects.

    }

}

想法是,您注冊類型的映射(包括類型中成員的類型)。然后,這些類型的集合的映射由 Automapper 自動處理。


查看完整回答
反對 回復 2023-09-24
  • 1 回答
  • 0 關注
  • 159 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號