简体   繁体   中英

Automapper in VB.Net

I am having a devil of a time attempting to translate the following piece of code from c# to VB.Net. I have little experience in VB.Net and all my searches have proven fruitless to date.

IMapper DataReaderMapper = new MapperConfiguration(cfg => {
    cfg.AddDataReaderMapping();
    cfg.CreateMap<IDataReader, MyDTO1>();
    cfg.CreateMap<IDataReader, MyDTO2>();
    cfg.CreateMap<IDataReader, MyDTO3>();
}).CreateMapper();

The code is using the Automapper and Automapper.Data nuget packages to map datatables to DTOs. I know the code works fine in c#.

My best guess was the following:

Dim DataReaderMapper As IMapper = New MapperConfiguration(Function(cfg) {cfg.AddDataReaderMapping(), cfg.CreateMap(Of IDataReader, MyDTO1)()}).CreateMapper()

The above results in an "Overload resolution failure" warning as I am obviously not passing in the arguments/parameters in the correct fashion/order. I can usually muddle by with most translations that I have to deal with but this one is stumping me. Any help would be appreciated.

According to https://converter.telerik.com/

    Dim DataReaderMapper As IMapper = New MapperConfiguration(Function(cfg)
                                                                  cfg.AddDataReaderMapping()
                                                                  cfg.CreateMap(Of IDataReader, MyDTO1)()
                                                                  cfg.CreateMap(Of IDataReader, MyDTO2)()
                                                                  cfg.CreateMap(Of IDataReader, MyDTO3)()
                                                              End Function).CreateMapper()

I was able to resolve by combining the responses of Nick Abbot's Telerik conversion and Craig's suggestion to swap out "Function" for "Sub" to get the following:

Dim DataReaderMapper As IMapper = New MapperConfiguration(Sub(cfg)
                                                              cfg.AddDataReaderMapping()
                                                              cfg.CreateMap(Of IDataReader, MyDTO1)()
                                                              cfg.CreateMap(Of IDataReader, MyDTO2)()
                                                              cfg.CreateMap(Of IDataReader, MyDTO3)()
                                                          End Sub).CreateMapper()

Thank you gentlemen for your assistance!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM