-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathLocalizableAttributeProperty.cs
56 lines (49 loc) · 1.87 KB
/
LocalizableAttributeProperty.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
using System;
using System.Reflection;
namespace CommandLine.Infrastructure
{
internal class LocalizableAttributeProperty
{
private string _propertyName;
private string _value;
private Type _type;
private PropertyInfo _localizationPropertyInfo;
public LocalizableAttributeProperty(string propertyName)
{
_propertyName = propertyName;
}
public string Value
{
get { return GetLocalizedValue(); }
set
{
_localizationPropertyInfo = null;
_value = value;
}
}
public Type ResourceType
{
set
{
_localizationPropertyInfo = null;
_type = value;
}
}
private string GetLocalizedValue()
{
if (String.IsNullOrEmpty(_value) || _type == null)
return _value;
if (_localizationPropertyInfo == null)
{
// Static class IsAbstract
if (!_type.IsVisible)
throw new ArgumentException($"Invalid resource type '{_type.FullName}'! {_type.Name} is not visible for the parser! Change resources 'Access Modifier' to 'Public'", _propertyName);
PropertyInfo propertyInfo = _type.GetProperty(_value, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Static);
if (propertyInfo == null || !propertyInfo.CanRead || (propertyInfo.PropertyType != typeof(string) && !propertyInfo.PropertyType.CanCast<string>()))
throw new ArgumentException($"Invalid resource property name! Localized value: {_value}", _propertyName);
_localizationPropertyInfo = propertyInfo;
}
return _localizationPropertyInfo.GetValue(null, null).Cast<string>();
}
}
}