编译时类型分配
我正在为我的 WPF/C# 应用程序开发一个自定义验证框架。
我想要做的是从声明视图模型的资源文件中检索字符串,但在实际的验证代码中它是它自己。此特定字符串与编辑 UI 表单上的标签使用的资源相同。
我的代码使用以下语法可以正常工作 -
[必需(TypeRes = typeof(资源))] 公共字符串RequiredStringWithDesc { 获取;放; }
但我正在寻找的是语法上看起来更干净的东西。我试图使用
const Type LocalRes = typeof(Resources); [必填(TypeRes = LocalRes)] 公共字符串RequiredStringWithDesc { 获取;放; }
关于更简单的语法有什么建议吗?旧的 c++ DEFINE 语句在这里可以很好地工作。
仅供参考:做这么多工作的原因与我们如何进行本地化和 UI 构建有关。
编辑回答几个关于我们为什么这样做的问题? 我们将使用资源文件中的相同字符串 -
- 在编辑屏幕上,这是标识字段的标签。
- 在数据模型中,如果存在验证错误,我们将使用它来正确标记日志文件中的问题。
- 在 Viewmodel 中,我们在验证错误消息中重复使用此标签,以向用户强调问题所在。
- 这是实时检查系统的一部分,一些故障模式直接与这些数据字段相关。因此,我们可以轻松获得正确本地化的标签以应用于运行时错误消息。
一般概念是,这简化了向用户呈现一致的消息,同时只创建一次内容。关于验证属性(以及这个问题),我们需要能够获取资源文件类型来加载正确的消息。
I'm working on a custom validation framework for my WPF/C# application.
What I'm looking to do is to retrieve strings from the resource file where the viewmodel is declared, but in the actual validation code it self. This particular string is the same resource used by label on the editing UI Form.
My code works fine with the following syntax -
[Required(TypeRes = typeof(Resources))] public string RequiredStringWithDesc { get; set; }
But what I"m looking for is something that is syntacticly cleaner looking. I was trying to use
const Type LocalRes = typeof(Resources); [Required(TypeRes = LocalRes)] public string RequiredStringWithDesc { get; set; }
Any suggestions on a simpler syntax? The old c++ DEFINE statement here would work well.
FYI: the reasons for going to this much work has to do with how we are doing localization and UI construction.
EDIT To answer a couple of questions about why are we doing this?
We are going to be using the same string from the resource file to -
- On the edit screen, this is the label to identify the field.
- In the datamodel, if there is a validation error, we are using this to correctly label the problem in the log file.
- In the Viewmodel, we are reusing this label in the validation error message to reinforce where the problem is to the user.
- This is part of a real time inspection system and some of the failure modes relate directly back to these data fields. So we can easily get the correctly localized label to apply to run-time fault messages
The general concept is that this simplifies presenting consistent messages to the user while only creating things once. With regards to validation attributes (and this question), we need to be able to get the Resource file type to load the correct message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个继承自RequiredAttribute的新属性类并设置默认值。
Create a new attribute class which inherits from the RequiredAttribute and set default values.