我在访问枚举的本地化值时遇到错误,例如
@(((FeeType)Model[i].FeeType).GetDescription())
Could not find any resources appropriate for the specified culture or the neutral culture.
Make sure "Resources.Admin.resources" was correctly embedded or linked into assembly "Auction" at compile time, or that all the satellite assemblies required are loadable and fully signed.
我的枚举是
public enum FeeType
{
[LocalizedDescriptionAttributre("Site_Fee_Type_MonthlyServiceFee", typeof(Resources.Admin))]
ClientFees = 1
}
和扩展类是
public class LocalizedDescriptionAttributre : DescriptionAttribute
{
private readonly string _resourceKey;
private readonly ResourceManager _resource;
public LocalizedDescriptionAttributre(string resourceKey, Type resourceType)
{
_resource = new ResourceManager(resourceType);
_resourceKey = resourceKey;
}
public override string Description
{
get
{
string displayName = _resource.GetString(_resourceKey);
return string.IsNullOrEmpty(displayName)
? string.Format("[[{0}]]", _resourceKey)
: displayName;
}
}
}
public static class EnumExtensions
{
public static string GetDescription(this Enum enumValue)
{
FieldInfo fi = enumValue.GetType().GetField(enumValue.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Description;
else
return enumValue.ToString();
}
}