API Reference

Every public type in the AutoMapperLite and AutoMapperLite.Interfaces namespaces, plus the Microsoft.Extensions.DependencyInjection registration extension.

IMapper

Namespace: AutoMapperLite.Interfaces. Registered by AddAutoMapperLite as a singleton; inject it wherever you need to map.

Map<TDestination>(object? source)

TDestination Map<TDestination>(object? source);

Maps source to a new instance of TDestination using the map registered for (source.GetType(), TDestination).

  • If source is null, returns default(TDestination) (null for reference types).
  • If TDestination is List<T>, T[], IList<T>, ICollection<T>, IReadOnlyList<T>, IReadOnlyCollection<T>, or IEnumerable<T>, source must implement IEnumerable; each element is mapped individually into a new collection of the requested shape.
  • Throws InvalidOperationException directly if no matching CreateMap was registered.
LocationViewModel dto = mapper.Map<LocationViewModel>(location);
List<SimpleDto> list  = mapper.Map<List<SimpleDto>>(sourceList);

Map<TSource, TDestination>(TSource source)

TDestination Map<TSource, TDestination>(TSource source);

Maps source to a new instance of TDestination using the map registered for the exact (TSource, TDestination) pair. Prefer this overload over Map<TDestination>(object) whenever the source type is known at the call site: it skips resolving source.GetType() and avoids boxing the return value, making it the fastest way to call AutoMapperLite.

  • If source is null, returns default(TDestination).
  • Dispatches by the compile-time type TSource, not source's runtime type — the one behavioral difference from the untyped overload.
  • If TDestination is List<TDestItem> and TSource is itself a collection (List<TSourceItem>, an array, or anything implementing IEnumerable<TSourceItem>), this maps every element without requiring a map registered for the collection types themselves — only for the item types.
  • Throws InvalidOperationException directly if no matching CreateMap was registered.
SimpleDto dto = mapper.Map<SimpleSource, SimpleDto>(source);
List<SimpleDto> list = mapper.Map<List<SimpleSource>, List<SimpleDto>>(sourceList);

IMapperConfig

Namespace: AutoMapperLite.Interfaces. Passed into Profile.Configure; registered by AddAutoMapperLite as a singleton. Thread-safe.

CreateMap<TSource, TDestination>()

MapBuilder<TSource, TDestination> CreateMap<TSource, TDestination>();

Registers a mapping between TSource and TDestination and returns a fluent MapBuilder for further configuration.

config.CreateMap<Country, CountryViewModel>();

GetMap<TSource, TDestination>()

MapBuilder<TSource, TDestination> GetMap<TSource, TDestination>();

Retrieves the builder previously registered via CreateMap. Throws InvalidOperationException if no map exists for that type pair. Used internally by Mapper; rarely called directly.

HasMap(Type sourceType, Type destType)

bool HasMap(Type sourceType, Type destType);

Returns whether a map is registered for the given type pair. Used internally by Mapper to decide whether a mismatched-type property (including the item types of a nested List<T>, collection-interface, or array-typed property) can be recursively mapped.

MapBuilder<TSource, TDestination>

Namespace: AutoMapperLite. Returned by CreateMap; configure it fluently.

ForMember<TMember>(destination, mapFunc)

MapBuilder<TSource, TDestination> ForMember<TMember>( Expression<Func<TDestination, TMember>> destination, Func<TSource, object?> mapFunc);

Configures mapping for a single, top-level destination property.

ParameterDescription
destinationExpression selecting a destination property, e.g. dest => dest.CountryViewModel. Throws ArgumentNullException if null.
mapFuncFunction producing the value to assign. Throws ArgumentNullException if null.
Important: mapFunc must return a value that is already assignable to the destination property's type. If the source and destination property types differ, call back into a Mapper built from the same config inside the function — see Examples → Nested object mapping. A mismatched value throws a descriptive InvalidOperationException naming the property path and the types involved.
config.CreateMap<SimpleSource, SimpleDestination>()
    .ForMember(dest => dest.Name, src => src.Name.ToUpperInvariant());

ForPath<TMember>(destination, mapFunc)

MapBuilder<TSource, TDestination> ForPath<TMember>( Expression<Func<TDestination, TMember>> destination, Func<TSource, object?> mapFunc);

Configures mapping for a nested destination property several levels deep. Functionally an alias of ForMember — the destination expression can reference any depth of nested properties, and the intermediate objects are instantiated automatically as needed.

ParameterDescription
destinationExpression selecting a nested destination path, e.g. dest => dest.OrganizationViewModel.CountryViewModel.Name.
mapFuncFunction producing the value to assign at that path, same rules as ForMember.

Multiple ForPath calls under the same top-level destination property are all applied to the same nested instance.

config.CreateMap<Location, LocationViewModel>()
    .ForPath(dest => dest.OrganizationViewModel.CountryViewModel.Name,
             src => src.Organization.Country.Name)
    .ForPath(dest => dest.OrganizationViewModel.OrgName,
             src => src.Organization.OrgName);

Profile

Namespace: AutoMapperLite.

abstract class Profile

public abstract void Configure(IMapperConfig config);

Base class for mapping profiles. Subclass it and override Configure to register your CreateMap calls. Must have a public parameterless constructor to be discovered by AddAutoMapperLite.

public class MyMappingProfile : Profile
{
    public override void Configure(IMapperConfig config)
    {
        config.CreateMap<Country, CountryViewModel>();
    }
}

ServiceCollectionExtensions

Namespace: Microsoft.Extensions.DependencyInjection (by convention, so it's discoverable alongside the other Add* extension methods without an extra using).

AddAutoMapperLite(Assembly)

IServiceCollection AddAutoMapperLite(this IServiceCollection services, Assembly assembly);

Scans assembly for concrete Profile subclasses with a public parameterless constructor, instantiates each and calls Configure against a shared MapperConfig, then registers:

  • IMapperConfig as a singleton (the populated config)
  • IMapper as a singleton (backed by Mapper)

Throws ArgumentNullException if assembly is null.

using System.Reflection;

builder.Services.AddAutoMapperLite(Assembly.GetExecutingAssembly());