BadScript 2
Loading...
Searching...
No Matches
LocalizableAttributeProperty.cs
Go to the documentation of this file.
1using System;
2using System.Reflection;
3
5{
7 {
8 private readonly string _propertyName;
9 private PropertyInfo _localizationPropertyInfo;
10 private Type _type;
11 private string _value;
12
13 public LocalizableAttributeProperty(string propertyName)
14 {
15 _propertyName = propertyName;
16 }
17
18 public string Value
19 {
20 get => GetLocalizedValue();
21 set
22 {
24 _value = value;
25 }
26 }
27
28 public Type ResourceType
29 {
30 set
31 {
33 _type = value;
34 }
35 }
36
37 private string GetLocalizedValue()
38 {
39 if (string.IsNullOrEmpty(_value) || _type == null)
40 {
41 return _value;
42 }
43
44 if (_localizationPropertyInfo == null)
45 {
46 // Static class IsAbstract
47 if (!_type.IsVisible)
48 {
49 throw new
50 ArgumentException($"Invalid resource type '{_type.FullName}'! {_type.Name} is not visible for the parser! Change resources 'Access Modifier' to 'Public'",
52 );
53 }
54
55 PropertyInfo propertyInfo =
56 _type.GetProperty(_value, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Static);
57
58 if (propertyInfo == null ||
59 !propertyInfo.CanRead ||
60 (propertyInfo.PropertyType != typeof(string) && !propertyInfo.PropertyType.CanCast<string>()))
61 {
62 throw new ArgumentException($"Invalid resource property name! Localized value: {_value}",
64 );
65 }
66
67 _localizationPropertyInfo = propertyInfo;
68 }
69
70 return _localizationPropertyInfo.GetValue(null, null)
71 .Cast<string>();
72 }
73 }
74}