MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.3k stars 328 forks source link

Fix Issues #536 -> Set properties with reflection if destination has init only properties. #545

Closed stormaref closed 1 year ago

stormaref commented 1 year ago

I've fixed #536 and wrote a test that regenerates the exact issue The test has been passed I add a method to "ClassAdapter" in this commit to generate reflection code for setting init only properties

The result is like this:

public _UserDto MapTo(_User p3, _UserDto p4)
        {
            if (p3 == null)
            {
                return null;
            }
            _UserDto result = p4 ?? new _UserDto();

            typeof(_UserDto).GetProperty("Id").SetValue(result, (object)p3.Id);
            typeof(_UserDto).GetProperty("Name").SetValue(result, p3.Name);
            return result;

        }
andrerav commented 1 year ago

Thank you!