/
ActionTypeAttribute.cs
50 lines (47 loc) · 1.81 KB
/
ActionTypeAttribute.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
using System;
using System.Linq;
using System.Reflection;
namespace Libplanet.Action
{
/// <summary>
/// Indicates that an action class (i.e., a class implementing
/// <see cref="IAction"/>) can be held by transactions and blocks.
/// It also gives an action class a <see cref="TypeIdentifier"/> for
/// serialization and deserialization.
/// </summary>
public class ActionTypeAttribute : Attribute
{
/// <summary>
/// Creates an <see cref="ActionTypeAttribute"/> with a given
/// <paramref name="typeIdentifier"/>.
/// </summary>
/// <param name="typeIdentifier">An action class's unique
/// identifier for serialization and deserialization.</param>
public ActionTypeAttribute(string typeIdentifier)
{
TypeIdentifier = typeIdentifier;
}
/// <summary>
/// An action class's unique identifier for serialization and
/// deserialization.
/// </summary>
public string TypeIdentifier { get; }
/// <summary>
/// Gets the <see cref="TypeIdentifier"/> for a given action class.
/// </summary>
/// <param name="actionType">A <see cref="Type"/> object of an action
/// class to know its annotated <see cref="TypeIdentifier"/>.</param>
/// <returns>The <see cref="TypeIdentifier"/> of the given
/// <paramref name="actionType"/> if it's annotated with
/// <see cref="ActionTypeAttribute"/>. If it's not annotated returns
/// <c>null</c>.</returns>
public static string ValueOf(Type actionType)
{
return actionType
.GetCustomAttributes()
.OfType<ActionTypeAttribute>()
.Select(attr => attr.TypeIdentifier)
.FirstOrDefault();
}
}
}