C#中的本地化属性参数 [英] Localized Attribute parameters in C#

查看:171
本文介绍了C#中的本地化属性参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,属性参数必须是常量表达式,typeof或数组创建表达式.

In C#, attribute parameters require to be a constant expression, typeof or array creation expression.

各种库(例如城堡验证器)都允许指定将看起来像本地化错误消息的内容传递给属性构造函数:

Various libraries, like for example Castle validator, allow specifying passing what seems like localized error messages to attribute constructor:

//this works
[ValidateNonEmpty("Can not be empty")]

//this does not compile
[ValidateNonEmpty(Resources.NonEmptyValidationMessage)]

有什么办法可以解决这个问题并使这些论点本地化吗?

Is there any way how to approach this problem and localize these arguments?

如果使用Castle Validator时没有解决方法,是否有类似于Castle Validator的验证库可以本地化验证消息?

In case there is no workaround for this when using Castle Validator, is there a validation library similar to Castle Validator that allows localization of validation messages?

我发现数据注释验证库如何解决此问题.非常优雅的解决方案: http://haacked.com/archive/2009/12/07/localizing-aspnetmvc-validation.aspx

I found how Data Annotations validation library approaches this problem. Very elegant solution: http://haacked.com/archive/2009/12/07/localizing-aspnetmvc-validation.aspx

推荐答案

开箱即用:

    [ValidateNonEmpty(
        FriendlyNameKey = "CorrectlyLocalized.Description",
        ErrorMessageKey = "CorrectlyLocalized.DescriptionValidateNonEmpty",
        ResourceType = typeof (Messages)
        )]
    public string Description { get; set; }

这篇关于C#中的本地化属性参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆