-
Notifications
You must be signed in to change notification settings - Fork 138
/
DuplicateActionTypeIdentifierException.cs
57 lines (53 loc) · 2.46 KB
/
DuplicateActionTypeIdentifierException.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
using System;
using System.Collections.Immutable;
using System.Linq;
namespace Libplanet.Action
{
/// <summary>
/// An exception that is thrown when multiple action types are associated with the same
/// <see cref="ActionTypeAttribute.TypeIdentifier"/>.
/// </summary>
/// <remarks>Note this cannot be serialized as it holds a set of <see cref="Type"/> objects
/// which are not serializable either.</remarks>
public sealed class DuplicateActionTypeIdentifierException : Exception
{
/// <summary>
/// Creates a new <see cref="DuplicateActionTypeIdentifierException"/> instance.
/// </summary>
/// <param name="message">Specifies an <see cref="Exception.Message"/>. Note that extra
/// informative text will be appended to this.</param>
/// <param name="typeIdentifier">The <see cref="ActionTypeAttribute.TypeIdentifier"/> string
/// that the <paramref name="duplicateActionTypes"/> are associated with.</param>
/// <param name="duplicateActionTypes">The set of multiple action types that are associated
/// with the same <paramref name="typeIdentifier"/>.</param>
public DuplicateActionTypeIdentifierException(
string message,
string typeIdentifier,
IImmutableSet<Type> duplicateActionTypes
)
: base($"{message}\n\n{GetExtraMessage(typeIdentifier, duplicateActionTypes)}")
{
TypeIdentifier = typeIdentifier;
DuplicateActionTypes = duplicateActionTypes;
}
/// <summary>
/// The <see cref="ActionTypeAttribute.TypeIdentifier"/> string that
/// the <see cref="DuplicateActionTypes"/> are associated with.
/// </summary>
public string TypeIdentifier { get; }
/// <summary>
/// The set of multiple action types that are associated with the same
/// <see cref="TypeIdentifier"/>.
/// </summary>
public IImmutableSet<Type> DuplicateActionTypes { get; }
private static string GetExtraMessage(
string typeIdentifier,
IImmutableSet<Type> duplicateActionTypes)
{
var types = duplicateActionTypes
.Select(t => t.FullName ?? $"{t.AssemblyQualifiedName ?? string.Empty}.(Unnamed)")
.OrderBy(s => s, StringComparer.InvariantCultureIgnoreCase);
return $"Type ID: {typeIdentifier}\nAssociated types:\n {string.Join("\n ", types)}";
}
}
}