Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的本地化属性参数#_C#_Validation_Localization_Attributes_Castle - Fatal编程技术网

C# C语言中的本地化属性参数#

C# C语言中的本地化属性参数#,c#,validation,localization,attributes,castle,C#,Validation,Localization,Attributes,Castle,在C#中,属性参数要求为常量表达式、typeof或数组创建表达式 各种库(例如Castle validator)允许指定将类似于本地化错误消息的内容传递给属性构造函数: //this works [ValidateNonEmpty("Can not be empty")] //this does not compile [ValidateNonEmpty(Resources.NonEmptyValidationMessage)] 有没有办法解决这个问题并将这些论点本地化 如果在使用Castl

在C#中,属性参数要求为常量表达式、typeof或数组创建表达式

各种库(例如Castle validator)允许指定将类似于本地化错误消息的内容传递给属性构造函数:

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

//this does not compile
[ValidateNonEmpty(Resources.NonEmptyValidationMessage)]
有没有办法解决这个问题并将这些论点本地化

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


编辑:我发现数据注释验证库是如何解决这个问题的。非常优雅的解决方案:

我们有一个类似的问题,尽管Castle没有。我们使用的解决方案只是定义一个从另一个属性派生的新属性,该属性使用常量字符串作为资源管理器的查找,如果没有找到,则返回到键字符串本身

[AttributeUsage(AttributeTargets.Class
  | AttributeTargets.Method
  | AttributeTargets.Property
  | AttributeTargets.Event)]
public class LocalizedIdentifierAttribute : ... {
  public LocalizedIdentifierAttribute(Type provider, string key)
    : base(...) {
    foreach (PropertyInfo p in provider.GetProperties(
      BindingFlags.Static | BindingFlags.NonPublic)) {
      if (p.PropertyType == typeof(System.Resources.ResourceManager)) {
        ResourceManager m = (ResourceManager) p.GetValue(null, null);

        // We found the key; use the value.
        return m.GetString(key);
      }
    }

    // We didn't find the key; use the key as the value.
    return key;
  }
}
用法类似于:

[LocalizedIdentifierAttribute(typeof(Resource), "Entities.FruitBasket")]
class FruitBasket {
  // ...
}

然后,每个特定于语言环境的资源文件都可以根据需要定义自己的
实体。水果篮
条目。

它是开箱即用的:

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

谢谢,那太好了,但是我使用的Castle Validator的ValidateNonEmptyAttribute版本没有这些属性。我只看到errorMessage、RunWhen、ExecutionOrder和FriendlyName。哪个版本的Castle validator具有这些属性?你能发布一个下载链接吗?有点难找,但是SVN版本有。谢谢你指出这一点!谢谢,这是一个非常优雅的解决方案,如何包装现有的库以允许这种行为。您还可以使用‘(Entities.FruitBasket)’,资源id可以更改。