Allow mapping of dynamic types using AutoMapper or similar?

前端 未结 4 1141
攒了一身酷
攒了一身酷 2020-12-02 05:46

I\'ve started to use https://github.com/robconery/massive for a project, I wonder if there is any mapping tool that allows support for Dynamic to static type mapping?

4条回答
  •  心在旅途
    2020-12-02 06:07

    Try Slapper.AutoMapper https://github.com/randyburden/Slapper.AutoMapper

    Slapper.AutoMapper maps dynamic data to static types

    It works for both dynamic and Dictionary which is awesome.

    Here's an example (taken from the URL above) showing how easily it works with Dictionary:

    public class Person
    {
        public int Id;
        public string FirstName;
        public string LastName;
    }
    
    [Test]
    public void CanMapMatchingFieldNamesWithEase()
    {
        // Arrange
        var dictionary = new Dictionary
                                {
                                    { "Id", 1 },
                                    { "FirstName", "Clark" },
                                    { "LastName", "Kent" }
                                };
    
        // Act
        var person = Slapper.AutoMapper.Map( dictionary );
    
        // Assert
        Assert.NotNull( person );
        Assert.That( person.Id == 1 );
        Assert.That( person.FirstName == "Clark" );
        Assert.That( person.LastName == "Kent" );
    }
    

提交回复
热议问题