Unosquare
    Show / Hide Table of Contents

    Class ObjectMapper

    Represents an AutoMapper-like object to map from one object type to another using defined properties map or using the default behaviour to copy same named properties from one object to another.

    The extension methods like CopyPropertiesTo use the default behaviour.

    Inheritance
    Object
    ObjectMapper
    Inherited Members
    Object.ToString()
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: Swan.Mappers
    Syntax
    public class ObjectMapper
    Examples

    The following code explains how to map an object's properties into an instance of type T.

    using Swan.Mappers;
    
    class Example
    {
        class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    
        static void Main()
        {
            var obj = new { Name = "John", Age = 42 };
    
            var person = Runtime.ObjectMapper.Map<Person>(obj);
        }
    }

    The following code explains how to explicitly map certain properties.

    using Swan.Mappers;
    
    class Example
    {
        class User
        {
            public string Name { get; set; }
            public Role Role { get; set; }
        }
    
        public class Role
        {
            public string Name { get; set; }
        }
    
        class UserDto
        {
            public string Name { get; set; }
            public string Role { get; set; }
        }
    
        static void Main()
        {
            // create a User object
            var person = 
                new User { Name = "Phillip", Role = new Role { Name = "Admin" } };
    
            // create an Object Mapper
            var mapper = new ObjectMapper();
    
            // map the User's Role.Name to UserDto's Role
            mapper.CreateMap<User, UserDto>()
                .MapProperty(d => d.Role, x => x.Role.Name);
    
            // apply the previous map and retrieve a UserDto object
            var destination = mapper.Map<UserDto>(person);
        }
    }

    Properties

    Current

    Gets the current.

    Declaration
    public static ObjectMapper Current { get; }
    Property Value
    Type Description
    ObjectMapper

    The current.

    Methods

    Copy(Nullable<IDictionary<String, Object>>, Object, Nullable<IEnumerable<String>>, String[])

    Copies the specified source.

    Declaration
    public static int Copy(IDictionary<string, object>? source, object target, IEnumerable<string>? propertiesToCopy = default(IEnumerable<string>? ), params string[] ignoreProperties)
    Parameters
    Type Name Description
    Nullable<IDictionary<String, Object>> source

    The source.

    Object target

    The target.

    Nullable<IEnumerable<String>> propertiesToCopy

    The properties to copy.

    String[] ignoreProperties

    The ignore properties.

    Returns
    Type Description
    Int32

    Copied properties count.

    Copy(Object, Object, Nullable<IEnumerable<String>>, String[])

    Copies the specified source.

    Declaration
    public static int Copy(object source, object target, IEnumerable<string>? propertiesToCopy = default(IEnumerable<string>? ), params string[] ignoreProperties)
    Parameters
    Type Name Description
    Object source

    The source.

    Object target

    The target.

    Nullable<IEnumerable<String>> propertiesToCopy

    The properties to copy.

    String[] ignoreProperties

    The ignore properties.

    Returns
    Type Description
    Int32

    Copied properties count.

    CreateMap<TSource, TDestination>()

    Creates the map.

    Declaration
    public ObjectMap<TSource, TDestination> CreateMap<TSource, TDestination>()
    Returns
    Type Description
    ObjectMap<TSource, TDestination>

    An object map representation of type of the destination property and type of the source property.

    Type Parameters
    Name Description
    TSource

    The type of the source.

    TDestination

    The type of the destination.

    Map<TDestination>(Object, Boolean)

    Maps the specified source.

    Declaration
    public TDestination Map<TDestination>(object source, bool autoResolve = true)
    Parameters
    Type Name Description
    Object source

    The source.

    Boolean autoResolve

    if set to true [automatic resolve].

    Returns
    Type Description
    TDestination

    A new instance of the map.

    Type Parameters
    Name Description
    TDestination

    The type of the destination.

    Comments

    Back to top Copyright © 2017-2019 Unosquare