-
Notifications
You must be signed in to change notification settings - Fork 3
/
ShowableAttribute.cs
111 lines (97 loc) · 3.43 KB
/
ShowableAttribute.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using Config4Net.Utils;
using System;
using System.Reflection;
namespace Config4Net.UI
{
/// <summary>
/// Annotates a property is a showable element in the UI.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
public class ShowableAttribute : Attribute
{
/// <summary>
/// Gets or sets the component name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the component label.
/// </summary>
public string Label { get; set; }
/// <summary>
/// Gets or sets the component description.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Gets or sets whether the component is mandatory.
/// </summary>
public bool Required { get; set; }
/// <summary>
/// Gets or sets the component condition that helps to validate the value
/// before it changes.
/// </summary>
public string Condition { get; set; }
/// <summary>
/// Gets or sets the component type that presents this property in UI.
/// </summary>
public Type ComponentType { get; set; }
/// <summary>
/// Create <see cref="ShowableAttribute"/>.
/// </summary>
public ShowableAttribute(string label = null)
{
Label = label;
}
}
/// <summary>
/// Contains extracted info from <see cref="ShowableAttribute"/>.
/// </summary>
public class ShowableInfo
{
/// <summary>
/// Gets or sets the component name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the component label.
/// </summary>
public string Label { get; set; }
/// <summary>
/// Gets or sets the component description.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Gets or sets whether the component is mandatory.
/// </summary>
public bool Required { get; set; }
/// <summary>
/// Gets or sets the component condition that helps to validate the value
/// before it changes.
/// </summary>
public string Condition { get; set; }
/// <summary>
/// Gets or sets the component type that presents this property in UI.
/// </summary>
public Type ComponentType { get; set; }
/// <summary>
/// Creates <see cref="ShowableInfo"/> from a giving property.
/// </summary>
/// <param name="memberInfo"></param>
/// <returns></returns>
public static ShowableInfo From(MemberInfo memberInfo)
{
var showableAttribute = memberInfo.GetCustomAttribute<ShowableAttribute>();
if (showableAttribute == null) return null;
return new ShowableInfo
{
Name = showableAttribute.Name,
Label = string.IsNullOrEmpty(showableAttribute.Label)
? StringUtils.ToFriendlyString(memberInfo.Name)
: showableAttribute.Label,
Description = showableAttribute.Description,
Required = showableAttribute.Required,
Condition = showableAttribute.Condition,
ComponentType = showableAttribute.ComponentType
};
}
}
}