전 몇 시간 전에 enum 값을 지역화하는 데 문제가있었습니다. 질문에 답을했는지 확신 할 수 없지만 적어도 다른 방법을 생각해보십시오. 나는 내가 LocalizationAttribute 설명 키를 가져오고 리소스 파일에 액세스하는 형식 변환기를 만든 다음에 열거 자체
[TypeConverter(typeof(EnumToLocalizedString))]
public enum ReviewReason
{
[LocalizationAttribute("ReviewReasonNewDocument")]
NewDocument = 1,
[LocalizationAttribute("ReviewReasonInternalAudit")]
InternalAudit = 2,
[LocalizationAttribute("ReviewReasonExternalAudit")]
ExternalAudit = 3,
[LocalizationAttribute("ReviewReasonChangedWorkBehaviour")]
ChangedWorkBehaviour = 4,
[LocalizationAttribute("ReviewReasonChangedWorkBehaviourBecauseOfComplaints")]
ChangedWorkBehaviourBecauseOfComplaints = 5,
[LocalizationAttribute("ReviewReasonMovedFromOlderSystem")]
MovedFromOlderSystem = 6,
[LocalizationAttribute("ReviewReasonPeriodicUpdate")]
PeriodicUpdate = 7,
[LocalizationAttribute("ReviewReasonDocumentChanged")]
DocumentChanged = 8
}
을 만들 거기에서 내 자신의 지역화 속성
/// <SUMMARY>
/// Attribute used for localization. Description field should contain a reference to the Resource file for correct localization
/// </SUMMARY>
public class LocalizationAttribute : Attribute
{
public LocalizationAttribute(string description)
{
this._description = description;
}
private string _description;
/// <SUMMARY>
/// Used to reference a resource key
/// </SUMMARY>
public string Description
{
get
{
return this._description;
}
}
}
를 작성하여 시작
현지화를 얻으려면 (속성 설명이 자원 키와 일치해야합니다 :))
public class EnumToLocalizedString : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return (sourceType.Equals(typeof(Enum)));
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return (destinationType.Equals(typeof(String)));
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (!destinationType.Equals(typeof(String)))
{
throw new ArgumentException("Can only convert to string.", "destinationType");
}
if (!value.GetType().BaseType.Equals(typeof(Enum)))
{
throw new ArgumentException("Can only convert an instance of enum.", "value");
}
string name = value.ToString();
object[] attrs = value.GetType().GetField(name).GetCustomAttributes(typeof(LocalizationAttribute), false);
if (attrs.Length != 1 !(attrs[0] is LocalizationAttribute))
{
throw new ArgumentException("Invalid enum argument");
}
return Handbok.Code.Resources.handbok.ResourceManager.GetString(((LocalizationAttribute)attrs[0]).Description);
}
}
,
마지막으로 나는이 경우 집합
public class ReviewReasonCollection
{
private static Collection<KEYVALUEPAIR<REVIEWREASON,>> _reviewReasons;
public static Collection<KEYVALUEPAIR<REVIEWREASON,>> AllReviewReasons
{
get
{
if (_reviewReasons == null)
{
_reviewReasons = new Collection<KEYVALUEPAIR<REVIEWREASON,>>();
TypeConverter t = TypeDescriptor.GetConverter(typeof(ReviewReason));
foreach (ReviewReason reviewReason in Enum.GetValues(typeof(ReviewReason)))
{
_reviewReasons.Add(new KeyValuePair<REVIEWREASON,>(reviewReason, t.ConvertToString(reviewReason)));
}
}
return _reviewReasons;
}
}
}
나는 원래 posted this solution on my blog입니다 TypeConverter가를 사용하는 클라이언트를 만들었습니다. 그것이 당신을 도와 줄 수 있기를 바랍니다 :)
rex를 편집하려면 기본 제공 Resource Editor를 사용하십시오. 그러나 그 경고는 무시할 수 있어야합니다, AFAICT. – psychotik
이것은 내장 편집기를 사용하고 있었으며 무시할 수 있으며 모든 것이 정상적으로 작동하는 것 같습니다. 유모를 좋아할 때 내가하지 말아야 할 일을하고있는 것처럼 느껴집니다. –