-
Notifications
You must be signed in to change notification settings - Fork 331
Ignoring members
Mapster will automatically map properties with the same names. You can ignore members by using the Ignore
method.
TypeAdapterConfig<TSource, TDestination>
.NewConfig()
.Ignore(dest => dest.Id);
You can ignore based on member information by IgnoreMember
command. Please see https://github.com/MapsterMapper/Mapster/wiki/Rule-based-member-mapping for more info.
TypeAdapterConfig.GlobalSettings.Default
.IgnoreMember((member, side) => !validTypes.Contains(member.Type));
You can ignore all non-mapped members by IgnoreNonMapped command. For example, we would like to map only Id and Name.
TypeAdapterConfig<TSource, TDestination>
.NewConfig()
.Map(dest => dest.Id, src => src.Id)
.Map(dest => dest.Name, src => src.Name)
.IgnoreNonMapped(true);
You can ignore member by decorate with [AdaptIgnore]
, and you can ignore custom attributes by IgnoreAttribute
command. Please see https://github.com/MapsterMapper/Mapster/wiki/Setting-by-attributes for more info.
public class Product {
public string Id { get; set; }
public string Name { get; set; }
[AdaptIgnore]
public decimal Price { get; set; }
}
You can ignore members conditionally, with condition based on source or target. When the condition is met, mapping of the property will be skipped altogether. This is the difference from custom Map
with condition, where destination is set to null
when condition is met.
TypeAdapterConfig<TSource, TDestination>
.NewConfig()
.IgnoreIf((src, dest) => !string.IsNullOrEmpty(dest.Name), dest => dest.Name);
You might would like to merge from input object, By default, Mapster will map all properties, even source properties containing null values. You can copy only properties that have values by using IgnoreNullValues
method.
TypeAdapterConfig<TSource, TDestination>
.NewConfig()
.IgnoreNullValues(true);
- Configuration
- Config inheritance
- Config instance
- Config location
- Config validation & compilation
- Config for nested mapping
- Custom member matching logic
- Constructor mapping
- Before & after mapping
- Setting values
- Shallow & merge mapping
- Recursive & object references
- Custom conversion logic
- Inheritance