forked from commandlineparser/commandline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalizableAttributeProperty.cs
More file actions
58 lines (52 loc) · 1.89 KB
/
Copy pathLocalizableAttributeProperty.cs
File metadata and controls
58 lines (52 loc) · 1.89 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
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))
throw new ArgumentException("Invalid resource property name! Localized value: {_value}", _propertyName);
_localizationPropertyInfo = propertyInfo;
}
return (string)_localizationPropertyInfo.GetValue(null, null);
}
}
}