#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AutoMapper.Extensions.EnumMapping@6.0.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
The AutoMapper.Extensions.EnumMapping library gives you control about your enum values mappings. It is possible to create a custom type converter for every enum.
This library supports mapping enums values like properties.
Either commands, from Package Manager Console or .NET Core CLI, will download and install AutoMapper.Extensions.EnumMapping. AutoMapper.Extensions.EnumMapping has no dependencies.
Usage
Install via NuGet first:
Install-Package AutoMapper.Extensions.EnumMapping
To use it:
For method CreateMap this library provide a ConvertUsingEnumMapping method. This method add all default mappings from source to destination enum values.
If you want to change some mappings, then you can use MapValue method. This is a chainable method.
Default the enum values are mapped by value (MapByValue()), but it is possible to map by name calling MapByName(). For enums which does not have same values and names, you can use MapByCustom(). Then you have to add a MapValue for every source enum value.
using AutoMapper.Extensions.EnumMapping;
public enum Source
{
Default = 0,
First = 1,
Second = 2
}
public enum Destination
{
Default = 0,
Second = 2
}
internal class YourProfile : Profile
{
public YourProfile()
{
CreateMap()
.ConvertUsingEnumMapping(opt => opt
// optional: .MapByValue() or MapByName(), without configuration MapByValue is used
.MapValue(Source.First, Destination.Default))
.ReverseMap(); // to support Destination to Source mapping, including custom mappings of ConvertUsingEnumMapping
}
}
...
Testing
AutoMapper provides a nice tooling for validating typemaps. This library adds an extra EnumMapperConfigurationExpressionExtensions.EnableEnumMappingValidation extension method to extend the existing AssertConfigurationIsValid() method to validate also the enum mappings.
To enable testing the enum mapping configuration:
public class MappingConfigurationsTests
{
[Fact]
public void WhenProfilesAreConfigured_ItShouldNotThrowException()
{
// Arrange
var config = new MapperConfiguration(configuration =>
{
configuration.EnableEnumMappingValidation();
configuration.AddMaps(typeof(AssemblyInfo).GetTypeInfo().Assembly);
});
// Assert
config.AssertConfigurationIsValid();
}
}
Product
VersionsCompatible and additional computed target framework versions.
.NET
net5.0net5.0 was computed. net5.0-windowsnet5.0-windows was computed. net6.0net6.0 was computed. net6.0-androidnet6.0-android was computed. net6.0-iosnet6.0-ios was computed. net6.0-maccatalystnet6.0-maccatalyst was computed. net6.0-macosnet6.0-macos was computed. net6.0-tvosnet6.0-tvos was computed. net6.0-windowsnet6.0-windows was computed. net7.0net7.0 was computed. net7.0-androidnet7.0-android was computed. net7.0-iosnet7.0-ios was computed. net7.0-maccatalystnet7.0-maccatalyst was computed. net7.0-macosnet7.0-macos was computed. net7.0-tvosnet7.0-tvos was computed. net7.0-windowsnet7.0-windows was computed. net8.0net8.0 is compatible. net8.0-androidnet8.0-android was computed. net8.0-browsernet8.0-browser was computed. net8.0-iosnet8.0-ios was computed. net8.0-maccatalystnet8.0-maccatalyst was computed. net8.0-macosnet8.0-macos was computed. net8.0-tvosnet8.0-tvos was computed. net8.0-windowsnet8.0-windows was computed. net9.0net9.0 is compatible. net9.0-androidnet9.0-android was computed. net9.0-browsernet9.0-browser was computed. net9.0-iosnet9.0-ios was computed. net9.0-maccatalystnet9.0-maccatalyst was computed. net9.0-macosnet9.0-macos was computed. net9.0-tvosnet9.0-tvos was computed. net9.0-windowsnet9.0-windows was computed. net10.0net10.0 is compatible. net10.0-androidnet10.0-android was computed. net10.0-browsernet10.0-browser was computed. net10.0-iosnet10.0-ios was computed. net10.0-maccatalystnet10.0-maccatalyst was computed. net10.0-macosnet10.0-macos was computed. net10.0-tvosnet10.0-tvos was computed. net10.0-windowsnet10.0-windows was computed.
.NET Core
netcoreapp2.0netcoreapp2.0 was computed. netcoreapp2.1netcoreapp2.1 was computed. netcoreapp2.2netcoreapp2.2 was computed. netcoreapp3.0netcoreapp3.0 was computed. netcoreapp3.1netcoreapp3.1 was computed.
.NET Standard
netstandard2.0netstandard2.0 is compatible. netstandard2.1netstandard2.1 was computed.
.NET Framework
net461net461 was computed. net462net462 was computed. net463net463 was computed. net47net47 was computed. net471net471 was computed. net472net472 was computed. net48net48 was computed. net481net481 was computed.
MonoAndroid
monoandroidmonoandroid was computed.
MonoMac
monomacmonomac was computed.
MonoTouch
monotouchmonotouch was computed.
Tizen
tizen40tizen40 was computed. tizen60tizen60 was computed.
A free library for ASP.NET Core 6+ Microservices development, with Model, Model Repository, Entity Framework Core and common Web Api features like CORS, Json serialization fixes, Swagger generation, JWT Authentication for simple and objective microservices development
A set of internal generic classes and extensions for working with caches, databases, queues, message brokers, files, reflection, errors, , pagination, collections, strings, and ASP NET CORE.
You can use this code in your projects as is. You can copy, modify and distribute it without any restrictions. You do not need to provide any copyrights or references.