这里有
DisplayAttribute
已添加到.NET 4.0中,允许您指定资源字符串:
[Display(Name = "UsernameField")]
string Username;
如果您还不能使用.NET 4.0,您可以编写自己的属性:
public class DisplayAttribute : DisplayNameAttribute
{
public DisplayAttribute(Type resourceManagerProvider, string resourceKey)
: base(LookupResource(resourceManagerProvider, resourceKey))
{
}
private static string LookupResource(Type resourceManagerProvider, string resourceKey)
{
var properties = resourceManagerProvider.GetProperties(
BindingFlags.Static | BindingFlags.NonPublic);
foreach (var staticProperty in properties)
{
if (staticProperty.PropertyType == typeof(ResourceManager))
{
var resourceManager = (ResourceManager)staticProperty
.GetValue(null, null);
return resourceManager.GetString(resourceKey);
}
}
return resourceKey;
}
}
你可以这样使用:
[Display(typeof(Resources.Resource), "UsernameField"),
string Username { get; set; }