-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from AutoMapper/ImprovingAnonymousTypeExpress…
…ionMapping Mapping MemberInit and Expression.New for anonymous types.
- Loading branch information
Showing
3 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/AutoMapper.Extensions.ExpressionMapping/AnonymousTypeFactory.cs
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,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
|
||
namespace AutoMapper.Extensions.ExpressionMapping | ||
{ | ||
internal static class AnonymousTypeFactory | ||
{ | ||
private static int classCount; | ||
|
||
public static Type CreateAnonymousType(IEnumerable<MemberInfo> memberDetails) | ||
=> CreateAnonymousType(memberDetails.ToDictionary(key => key.Name, element => element.GetMemberType())); | ||
|
||
public static Type CreateAnonymousType(IDictionary<string, Type> memberDetails) | ||
{ | ||
AssemblyName dynamicAssemblyName = new AssemblyName("TempAssembly.AutoMapper.Extensions.ExpressionMapping"); | ||
AssemblyBuilder dynamicAssembly = AssemblyBuilder.DefineDynamicAssembly(dynamicAssemblyName, AssemblyBuilderAccess.Run); | ||
ModuleBuilder dynamicModule = dynamicAssembly.DefineDynamicModule("TempAssembly.AutoMapper.Extensions.ExpressionMapping"); | ||
TypeBuilder typeBuilder = dynamicModule.DefineType(GetAnonymousTypeName(), TypeAttributes.Public); | ||
MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; | ||
|
||
var builders = memberDetails.Select | ||
( | ||
info => | ||
{ | ||
Type memberType = info.Value; | ||
string memberName = info.Key; | ||
return new | ||
{ | ||
FieldBuilder = typeBuilder.DefineField(string.Concat("_", memberName), memberType, FieldAttributes.Private), | ||
PropertyBuilder = typeBuilder.DefineProperty(memberName, PropertyAttributes.HasDefault, memberType, null), | ||
GetMethodBuilder = typeBuilder.DefineMethod(string.Concat("get_", memberName), getSetAttr, memberType, Type.EmptyTypes), | ||
SetMethodBuilder = typeBuilder.DefineMethod(string.Concat("set_", memberName), getSetAttr, null, new Type[] { memberType }) | ||
}; | ||
} | ||
); | ||
|
||
builders.ToList().ForEach(builder => | ||
{ | ||
ILGenerator getMethodIL = builder.GetMethodBuilder.GetILGenerator(); | ||
getMethodIL.Emit(OpCodes.Ldarg_0); | ||
getMethodIL.Emit(OpCodes.Ldfld, builder.FieldBuilder); | ||
getMethodIL.Emit(OpCodes.Ret); | ||
ILGenerator setMethodIL = builder.SetMethodBuilder.GetILGenerator(); | ||
setMethodIL.Emit(OpCodes.Ldarg_0); | ||
setMethodIL.Emit(OpCodes.Ldarg_1); | ||
setMethodIL.Emit(OpCodes.Stfld, builder.FieldBuilder); | ||
setMethodIL.Emit(OpCodes.Ret); | ||
builder.PropertyBuilder.SetGetMethod(builder.GetMethodBuilder); | ||
builder.PropertyBuilder.SetSetMethod(builder.SetMethodBuilder); | ||
}); | ||
|
||
return typeBuilder.CreateTypeInfo().AsType(); | ||
} | ||
|
||
private static string GetAnonymousTypeName() | ||
=> $"AnonymousType{++classCount}"; | ||
} | ||
} |
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