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
sourceisnull, returnsdefault(TDestination)(nullfor reference types). - If
TDestinationisList<T>,T[],IList<T>,ICollection<T>,IReadOnlyList<T>,IReadOnlyCollection<T>, orIEnumerable<T>,sourcemust implementIEnumerable; each element is mapped individually into a new collection of the requested shape. - Throws
InvalidOperationExceptiondirectly if no matchingCreateMapwas 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
sourceisnull, returnsdefault(TDestination). - Dispatches by the compile-time type
TSource, notsource's runtime type — the one behavioral difference from the untyped overload. - If
TDestinationisList<TDestItem>andTSourceis itself a collection (List<TSourceItem>, an array, or anything implementingIEnumerable<TSourceItem>), this maps every element without requiring a map registered for the collection types themselves — only for the item types. - Throws
InvalidOperationExceptiondirectly if no matchingCreateMapwas 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.
| Parameter | Description |
|---|---|
destination | Expression selecting a destination property, e.g. dest => dest.CountryViewModel. Throws ArgumentNullException if null. |
mapFunc | Function producing the value to assign. Throws ArgumentNullException if null. |
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.
| Parameter | Description |
|---|---|
destination | Expression selecting a nested destination path, e.g. dest => dest.OrganizationViewModel.CountryViewModel.Name. |
mapFunc | Function 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:
IMapperConfigas a singleton (the populated config)IMapperas a singleton (backed byMapper)
Throws ArgumentNullException if assembly is null.
using System.Reflection;
builder.Services.AddAutoMapperLite(Assembly.GetExecutingAssembly());