Skip to content

Commit

Permalink
fix #256 exclude backing fields from mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
chaowlert committed Aug 5, 2020
1 parent 2637d8c commit d6e70e8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
37 changes: 33 additions & 4 deletions src/Mapster.Tests/WhenMappingPrivateFieldsAndProperties.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;

namespace Mapster.Tests
Expand Down Expand Up @@ -130,15 +131,32 @@ public void Should_Map_To_Private_Properties_Using_Include()
dto.Name.ShouldBe(customerName);
}

private void SetUpMappingNonPublicFields<TSource, TDestination>()
[TestMethod]
public void Test_Dictionary()
{
var config = new TypeAdapterConfig();
config.ForType<IDictionary<string, object>, Pet>()
.EnableNonPublicMembers(true);

var pet = new Dictionary<string, object>
{
{ "Name", "Fluffy" },
{ "Color", "White" }
}.Adapt<Pet>(config);

pet.Name.ShouldBe("Fluffy");
pet.GetPrivateColor().ShouldBe("White");
}

private static void SetUpMappingNonPublicFields<TSource, TDestination>()
{
TypeAdapterConfig<TSource, TDestination>
.NewConfig()
.EnableNonPublicMembers(true)
.NameMatchingStrategy(NameMatchingStrategy.Flexible);
}

private void SetUpMappingNonPublicProperties<TSource, TDestination>()
private static void SetUpMappingNonPublicProperties<TSource, TDestination>()
{
TypeAdapterConfig<TSource, TDestination>
.NewConfig()
Expand All @@ -149,7 +167,7 @@ private void SetUpMappingNonPublicProperties<TSource, TDestination>()

public class CustomerWithPrivateField
{
private int _id;
private readonly int _id;
public string Name { get; private set; }

private CustomerWithPrivateField() { }
Expand Down Expand Up @@ -210,6 +228,17 @@ public class CustomerDTO
public string Name { get; set; }
}

public class Pet
{
public string Name { get; set; }

private string Color { get; set; }

public string GetPrivateColor()
{
return this.Color;
}
}
#endregion
}
}
7 changes: 6 additions & 1 deletion src/Mapster/Settings/ShouldMapMember.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using Mapster.Models;
using System;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace Mapster
{
public static class ShouldMapMember
{
public static readonly Func<IMemberModel, MemberSide, bool?> AllowNonPublic = (model, _) => (model.AccessModifier & AccessModifier.NonPublic) != 0 ? (bool?)true : null;
public static readonly Func<IMemberModel, MemberSide, bool?> AllowNonPublic = (model, _) =>
(model.AccessModifier & AccessModifier.NonPublic) == 0
? (bool?) null
: !(model.Info is FieldInfo) || model.GetCustomAttribute<CompilerGeneratedAttribute>() == null;
public static readonly Func<IMemberModel, MemberSide, bool?> AllowPublic = (model, _) => model.AccessModifier == AccessModifier.Public ? (bool?)true : null;
public static readonly Func<IMemberModel, MemberSide, bool?> IgnoreAdaptIgnore = (model, side) =>
{
Expand Down

0 comments on commit d6e70e8

Please sign in to comment.