Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map different enums by name #153

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions ExpressMapper NETCORE/ExpressMapper NETCORE.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>1.9.1.0</VersionPrefix>
<TargetFramework>netstandard1.6</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>ExpressMapper</AssemblyName>
<PackageId>ExpressMapper NETCORE</PackageId>
<NetStandardImplicitPackageVersion>1.6.0</NetStandardImplicitPackageVersion>
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.6' ">
<PackageReference Include="System.Linq.Expressions" Version="4.1.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.0.1" />
<PackageReference Include="System.Linq.Queryable" Version="4.0.1" />
<PackageReference Include="System.Globalization.Extensions" Version="4.0.1" />
</ItemGroup>

</Project>
19 changes: 0 additions & 19 deletions ExpressMapper NETCORE/ExpressMapper NETCORE.xproj

This file was deleted.

1 change: 1 addition & 0 deletions ExpressMapper NETCORE/IMappingServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ public interface IMappingServiceProvider
Dictionary<int, IList<long>> CustomMappingsBySource { get; }
IQueryable<TN> Project<T, TN>(IQueryable<T> source);
bool CaseSensetiveMemberMap { get; set; }
bool MapEnumsByName { get; set; }
}
}
5 changes: 5 additions & 0 deletions ExpressMapper NETCORE/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,10 @@ public static void MemberCaseSensitiveMap(bool caseSensitive)
{
Instance.CaseSensetiveMemberMap = caseSensitive;
}

public static void MapEnumsByName()
{
Instance.MapEnumsByName = true;
}
}
}
26 changes: 25 additions & 1 deletion ExpressMapper NETCORE/MappingServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public Expression GetMemberMappingExpression(Expression left, Expression right,

if (destType.GetInfo().IsEnum && sourceType.GetInfo().IsEnum)
{
return Expression.Assign(left, Expression.Convert(Expression.Convert(right, _enumImplicitConversionType), destType));
return GetEnumMappingExpression(left, right, sourceType, destType);
}

if (typeof(IConvertible).GetInfo().IsAssignableFrom(destType) &&
Expand Down Expand Up @@ -201,6 +201,30 @@ public Expression GetMemberMappingExpression(Expression left, Expression right,
return conditionalExpression;
}

private Expression GetEnumMappingExpression(Expression left, Expression right, Type sourceType, Type destType)
{
if (!MappingServiceProvider.MapEnumsByName)
{
return Expression.Assign(left, Expression.Convert(Expression.Convert(right, _enumImplicitConversionType), destType));
}

var getNameTypeParam = Expression.Parameter(typeof(Type));
var getNameObjectParam = Expression.Parameter(typeof(object));
var getNameCall = Expression.Call(typeof(Enum), nameof(Enum.GetName), null, getNameTypeParam, getNameObjectParam);
var getNameLambda = Expression.Lambda(getNameCall, getNameTypeParam, getNameObjectParam);

var parseTypeParam = Expression.Parameter(typeof(Type));
var parseStringParam = Expression.Parameter(typeof(string));
var parseCall = Expression.Call(typeof(Enum), nameof(Enum.Parse), null, parseTypeParam, parseStringParam);
var parseLambda = Expression.Lambda(parseCall, parseTypeParam, parseStringParam);

var rightAsObject = Expression.Convert(right, typeof(object));
var getNameInvoke = Expression.Invoke(getNameLambda, Expression.Constant(sourceType), rightAsObject);
var parseInvoke = Expression.Invoke(parseLambda, Expression.Constant(destType), getNameInvoke);

return Expression.Assign(left, Expression.Convert(parseInvoke, destType));
}

private static Expression CreateAssignExpression(Expression setMethod, Expression getMethod, Type setType, Type setNullableType, Type getNullableType)
{
var left = setMethod;
Expand Down
2 changes: 2 additions & 0 deletions ExpressMapper NETCORE/MappingServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public IQueryable<TN> Project<T, TN>(IQueryable<T> source)
}

public bool CaseSensetiveMemberMap { get; set; }
public bool MapEnumsByName { get; set; }

public IMemberConfiguration<T, TN> Register<T, TN>()
{
Expand Down Expand Up @@ -225,6 +226,7 @@ public void Reset()
mappingService.Reset();
}
CaseSensetiveMemberMap = false;
MapEnumsByName = false;
}
}

Expand Down
23 changes: 0 additions & 23 deletions ExpressMapper NETCORE/project.json

This file was deleted.

Loading