Skip to content

Commit 07a6f58

Browse files
Merge pull request #55 from AutoMapper/HashingMembers
GetHashCode call by type
2 parents 60bc1a0 + eda7e98 commit 07a6f58

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

src/AutoMapper.Collection.Tests/MapCollectionWithEqualityTests.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,84 @@ public void Should_Work_With_Null_Destination()
210210
Mapper.Map<List<Thing>>(dtos).Should().HaveSameCount(dtos);
211211
}
212212

213+
public void Should_Work_With_Comparing_String_Types()
214+
{
215+
Mapper.Initialize(cfg =>
216+
{
217+
cfg.AddCollectionMappers();
218+
cfg.CreateMap<Charge, SaleCharge>()
219+
.ForMember(d => d.SaleId, o => o.Ignore())
220+
.EqualityComparison((c, sc) => sc.Category == c.Category && sc.Description == c.Description);
221+
222+
cfg.CreateMap<SaleCharge, Charge>()
223+
.ConstructUsing(
224+
(saleCharge => new Charge(saleCharge.Category, saleCharge.Description, saleCharge.Value)))
225+
.EqualityComparison((sc, c) => sc.Category == c.Category && sc.Description == c.Description);
226+
});
227+
228+
var dto = new Charge("catagory", "description", 5);
229+
var entity = new SaleCharge { Category = dto.Category, Description = dto.Description };
230+
var entityCollection = new List<SaleCharge> { entity };
231+
232+
Mapper.Map(new[] { dto }, entityCollection);
233+
234+
entity.ShouldBeEquivalentTo(entityCollection[0]);
235+
}
236+
237+
public class Charge
238+
{
239+
public Charge(string category, string description, decimal value)
240+
{
241+
Category = category;
242+
Description = description;
243+
Value = value;
244+
}
245+
246+
public string Category { get; }
247+
public string Description { get; }
248+
public decimal Value { get; }
249+
250+
public override string ToString()
251+
{
252+
return $"{Category}|{Description}|{Value}";
253+
}
254+
255+
public override int GetHashCode()
256+
{
257+
return $"{Category}|{Description}|{Value}".GetHashCode();
258+
}
259+
260+
public override bool Equals(object obj)
261+
{
262+
if (ReferenceEquals(this, obj))
263+
{
264+
return true;
265+
}
266+
267+
if (ReferenceEquals(null, obj))
268+
{
269+
return false;
270+
}
271+
272+
var _obj = obj as Charge;
273+
274+
if (_obj == null)
275+
{
276+
return false;
277+
}
278+
279+
return Category == _obj.Category && Description == _obj.Description && Value == _obj.Value;
280+
}
281+
}
282+
283+
public class SaleCharge
284+
{
285+
public Guid SaleId { get; set; }
286+
public string Category { get; set; }
287+
public string Description { get; set; }
288+
public decimal Value { get; set; }
289+
}
290+
213291
public void Should_Be_Instanced_Based()
214292
{
215293
Mapper.Initialize(x =>

src/AutoMapper.Collection/EquivalencyExpression/ExpressionExtentions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,11 @@ public static Expression<Func<T, int>> GetHashCodeExpression<T>(this List<Expres
4848
var returnTarget = Expression.Label(typeof(int));
4949
var returnExpression = Expression.Return(returnTarget, Expression.Convert(hashVariable, typeof(int)), typeof(int));
5050
var returnLabel = Expression.Label(returnTarget, Expression.Constant(-1));
51-
52-
var getHashCodeMethod = typeof(T).GetDeclaredMethod(nameof(GetHashCode));
53-
51+
5452
var expressions = new List<Expression>();
5553
foreach (var member in members)
5654
{
57-
var callGetHashCode = Expression.Call(member, getHashCodeMethod);
55+
var callGetHashCode = Expression.Call(member, member.Type.GetDeclaredMethod(nameof(GetHashCode)));
5856
var convertHashCodeToInt64 = Expression.Convert(callGetHashCode, typeof(long));
5957
if (expressions.Count == 0)
6058
{

0 commit comments

Comments
 (0)