如何从代码中检索数据注释? (以编程方式)
我正在使用 System.ComponentModel.DataAnnotations 为我的 Entity Framework 4.1 项目提供验证。
例如:
public class Player
{
[Required]
[MaxLength(30)]
[Display(Name = "Player Name")]
public string PlayerName { get; set; }
[MaxLength(100)]
[Display(Name = "Player Description")]
public string PlayerDescription{ get; set; }
}
我需要检索 Display.Name
注释值以在消息中显示它,例如 The selected "Player Name" is Frank.
====== =================================================== =========================
为什么我需要检索注释的另一个例子:
var playerNameTextBox = new TextBox();
playerNameTextBox.MaxLength = GetAnnotation(myPlayer.PlayerName, MaxLength);
我怎样才能做到这一点?
I'm using System.ComponentModel.DataAnnotations
to provide validation for my Entity Framework 4.1 project.
For example:
public class Player
{
[Required]
[MaxLength(30)]
[Display(Name = "Player Name")]
public string PlayerName { get; set; }
[MaxLength(100)]
[Display(Name = "Player Description")]
public string PlayerDescription{ get; set; }
}
I need to retrieve the Display.Name
annotation value to show it in a message such as The chosen "Player Name" is Frank.
=================================================================================
Another example of why I could need to retrieve annotations:
var playerNameTextBox = new TextBox();
playerNameTextBox.MaxLength = GetAnnotation(myPlayer.PlayerName, MaxLength);
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
扩展方法:
代码:
Extension method:
Code:
试试这个:
try this:
以下是一些可用于获取 MaxLength 或任何其他属性的静态方法。
使用静态方法...
或者在实例上使用可选的扩展方法...
或者对任何其他属性使用完整的静态方法(例如 StringLength)...
受到此处答案的启发...
https://stackoverflow.com/a/32501356/324479
Here are some static methods you can use to get the MaxLength, or any other attribute.
Using the static method...
Or using the optional extension method on an instance...
Or using the full static method for any other attribute (StringLength for example)...
Inspired by the answer here...
https://stackoverflow.com/a/32501356/324479
这就是我做类似事情的方式
This is how I have done something similar
因为 https://stackoverflow.com/a/7027791/7173655 上的接受答案仍然使用魔术常量,所以我分享我的基于链接答案的代码:
扩展方法:
没有魔术常量的使用(确保重构确实伤害较小):
Because the acceptet answer on https://stackoverflow.com/a/7027791/7173655 still uses magic constants, I share my code based on the linked answer:
Extension method:
Usage without magic constants (assuring refactoring does hurt less):
我认为这个例子 https://github.com/TeteStorm/DataAnnotationScan 非常有用。
我只是为了让 EF 在我的模型装配中使用数据注释,但请随意 fork 并根据需要进行更改。
更改以下方法 HasEFDataAnnotation 并享受乐趣!
https://github.com/TeteStorm/DataAnnotationScan
I think so that this example https://github.com/TeteStorm/DataAnnotationScan can be very usefull.
I maide just to get EF used Data Annotations at my model assembly, but feel free for fork and change as you need.
Change the below method HasEFDataAnnotaion and have fun!
https://github.com/TeteStorm/DataAnnotationScan
修复了使用元数据类与 MetadataTypeAttribute 的问题,来自此处
a Fix for using metadata Class with MetadataTypeAttribute from here