-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split EnumAdapter+StringAdapter+ObjectAdapter, fix dynamic
- Loading branch information
Showing
13 changed files
with
192 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Mapster.Utils; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Mapster.Adapters | ||
{ | ||
internal class EnumAdapter : PrimitiveAdapter | ||
{ | ||
protected override int Score => -109; //must do before StringAdapter | ||
protected override bool CheckExplicitMapping => false; | ||
|
||
protected override bool CanMap(PreCompileArgument arg) | ||
{ | ||
return arg.SourceType.GetTypeInfo().IsEnum || arg.DestinationType.GetTypeInfo().IsEnum; | ||
} | ||
|
||
protected override Expression ConvertType(Expression source, Type destinationType, CompileArgument arg) | ||
{ | ||
var srcType = source.Type; | ||
if (destinationType == typeof(string)) | ||
{ | ||
var method = typeof(Enum<>).MakeGenericType(srcType).GetMethod("ToString", new[] { srcType }); | ||
return Expression.Call(method, source); | ||
} | ||
else if (srcType == typeof(string)) | ||
{ | ||
var method = typeof(Enum<>).MakeGenericType(destinationType).GetMethod("Parse", new[] { typeof(string) }); | ||
return Expression.Call(method, source); | ||
} | ||
else if (destinationType.GetTypeInfo().IsEnum && srcType.GetTypeInfo().IsEnum && arg.Settings.MapEnumByName == true) | ||
{ | ||
var method = typeof(Enum<>).MakeGenericType(srcType).GetMethod("ToString", new[] { srcType }); | ||
var tostring = Expression.Call(method, source); | ||
var methodParse = typeof(Enum<>).MakeGenericType(destinationType).GetMethod("Parse", new[] { typeof(string) }); | ||
|
||
return Expression.Call(methodParse, tostring); | ||
} | ||
|
||
|
||
return base.ConvertType(source, destinationType, arg); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.