【发布时间】:2011-08-11 14:25:30
【问题描述】:
我正在使用 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 注释值以在消息中显示它,例如选择的“玩家名称”是弗兰克。
================================================ ====================================
我可能需要检索注释的另一个示例:
var playerNameTextBox = new TextBox();
playerNameTextBox.MaxLength = GetAnnotation(myPlayer.PlayerName, MaxLength);
我该怎么做?
【问题讨论】:
-
您想使用反射来实现这一点。可以找到一个可行的解决方案here。
-
请看这篇帖子stackoverflow.com/questions/803221/… 它向您展示了如何使用反射来做到这一点。
标签: c# entity-framework-4.1 data-annotations