我正在为我的WPF / C#应用程序开发一个自定义验证框架。
我要做的是从声明viewmodel的资源文件中检索字符串,但在实际的验证代码中它是自己的。此特定字符串与编辑UI表单上的标签使用的资源相同。
我的代码可以正常使用以下语法 -
[Required(TypeRes = typeof(Resources))] public string RequiredStringWithDesc { get; set; }
但我正在寻找的东西在语法上更清晰。我试图使用
const Type LocalRes = typeof(Resources); [Required(TypeRes = LocalRes)] public string RequiredStringWithDesc { get; set; }
有关更简单语法的任何建议吗?这里的旧c ++ DEFINE语句可以很好地工作。
仅供参考:开展这项工作的原因与我们如何进行本地化和UI构建有关。
编辑回答几个问题我们为什么要这样做? 我们将使用资源文件中相同的字符串 -
一般的概念是,这简化了向用户呈现一致的消息,同时只创建了一次。关于验证属性(和这个问题),我们需要能够获取资源文件类型以加载正确的消息。
答案 0 :(得分:0)
创建一个新的属性类,该类继承自RequiredAttribute并设置默认值。
public class LocalizedRequiredAttribute : RequiredAttribute {
public LocalizedRequiredAttribute() { /* TypeDef = typeof(Resources);*/ }
}
public class MyModel {
[LocalizedRequired]
public string RequiredStringWithDesc { get; set; }
}