-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathVerifyConfig.cs
65 lines (59 loc) · 2.52 KB
/
VerifyConfig.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace KellermanSoftware.CompareNetObjects
{
/// <summary>
/// Used internally to verify the config settings before comparing
/// </summary>
public class VerifyConfig
{
/// <summary>
/// Verifies the specified configuration.
/// </summary>
/// <param name="config">The configuration.</param>
public void Verify(ComparisonConfig config)
{
VerifySpec(config);
}
/// <summary>
/// Verifies the collection matching spec.
/// </summary>
/// <param name="config">The configuration.</param>
/// <exception cref="Exception">
/// </exception>
public void VerifySpec(ComparisonConfig config)
{
if (config.CollectionMatchingSpec == null)
return;
foreach (var kvp in config.CollectionMatchingSpec)
{
if (TypeHelper.IsIList(kvp.Key))
{
string msg = string.Format(
"Collection Matching Spec Type {0} should be a class, not a List. Expected something like Customer, not List<Customer>. See https://github.com/GregFinzer/Compare-Net-Objects/wiki/Comparing-Lists-of-Different-Lengths",
kvp.Key.Name);
throw new ArgumentException(msg, nameof(config));
}
if (!TypeHelper.IsClass(kvp.Key) && !TypeHelper.IsInterface(kvp.Key))
{
string msg = string.Format(
"Collection matching spec Type {0} should be a class or an interface. See https://github.com/GregFinzer/Compare-Net-Objects/wiki/Comparing-Lists-of-Different-Lengths",
kvp.Key.Name);
throw new ArgumentException(msg, nameof(config));
}
List<PropertyInfo> propertyInfos = Cache.GetPropertyInfo(config, kvp.Key).ToList();
foreach (var index in kvp.Value)
{
if (propertyInfos.All(o => o.Name != index))
{
string msg = string.Format("Collection Matching Spec cannot find property {0} of type {1}",
index, kvp.Key.Name);
throw new ArgumentException(msg, nameof(config));
}
}
}
}
}
}