在asp.net mvc中本地化枚举属性

时间:2015-05-10 09:12:24

标签: c# .net asp.net-mvc

我有枚举属性

public enum MyType
{
    House = 0, 
    Apartment = 1
}

现在我想使用本地化的resx字符串本地化这个枚举,所以我添加

public enum MyType
{
    [LocalizedName("House", typeof(LocalizedUIStrings))]
     House = 0, 
    [LocalizedName("Apartment ", typeof(LocalizedUIStrings))]
     Apartment = 1
 }

和localizedname helper class

public class LocalizedNameAttribute : Attribute
{
    private readonly Type _resourceType;
    private readonly string _resourceKey;

    public LocalizedNameAttribute(string resourceKey, Type resourceType)
    {
       _resourceType = resourceType;
       _resourceKey = resourceKey;
       DisplayName = (string)_resourceType
           .GetProperty(_resourceKey, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
          .GetValue(null, null);
    }
    public string DisplayName { get; private set; }
}

在视图中我试图使用像这样的本地化属性

@Html.DisplayFor(modelItem => item.MyType)

它仍然返回非本地化字符串,没有错误(在编译或运行时)。 我在这里做错了什么?

0 个答案:

没有答案