-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
871 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/CommandLine/Infrastructure/LocalizableAttributeProperty.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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", _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", _propertyName); | ||
_localizationPropertyInfo = propertyInfo; | ||
} | ||
return (string)_localizationPropertyInfo.GetValue(null, null); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.