asm*_*smo 50 c# data-annotations entity-framework-4.1
我正在使用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; }
}
Run Code Online (Sandbox Code Playgroud)
我需要检索Display.Name注释值以在消息中显示它,例如所选的"玩家名称"是Frank.
================================================== ===============================
我需要检索注释的另一个例子:
var playerNameTextBox = new TextBox();
playerNameTextBox.MaxLength = GetAnnotation(myPlayer.PlayerName, MaxLength);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
jga*_*fin 82
扩展方法:
public static T GetAttributeFrom<T>(this object instance, string propertyName) where T : Attribute
{
var attrType = typeof(T);
var property = instance.GetType().GetProperty(propertyName);
return (T)property .GetCustomAttributes(attrType, false).First();
}
Run Code Online (Sandbox Code Playgroud)
码:
var name = player.GetAttributeFrom<DisplayAttribute>("PlayerDescription").Name;
var maxLength = player.GetAttributeFrom<MaxLengthAttribute>("PlayerName").Length;
Run Code Online (Sandbox Code Playgroud)
小智 7
试试这个:
((DisplayAttribute)
(myPlayer
.GetType()
.GetProperty("PlayerName")
.GetCustomAttributes(typeof(DisplayAttribute),true)[0])).Name;
Run Code Online (Sandbox Code Playgroud)
以下是一些可用于获取 MaxLength 或任何其他属性的静态方法。
using System;
using System.Linq;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
public static class AttributeHelpers {
public static Int32 GetMaxLength<T>(Expression<Func<T,string>> propertyExpression) {
return GetPropertyAttributeValue<T,string,MaxLengthAttribute,Int32>(propertyExpression,attr => attr.Length);
}
//Optional Extension method
public static Int32 GetMaxLength<T>(this T instance,Expression<Func<T,string>> propertyExpression) {
return GetMaxLength<T>(propertyExpression);
}
//Required generic method to get any property attribute from any class
public static TValue GetPropertyAttributeValue<T, TOut, TAttribute, TValue>(Expression<Func<T,TOut>> propertyExpression,Func<TAttribute,TValue> valueSelector) where TAttribute : Attribute {
var expression = (MemberExpression)propertyExpression.Body;
var propertyInfo = (PropertyInfo)expression.Member;
var attr = propertyInfo.GetCustomAttributes(typeof(TAttribute),true).FirstOrDefault() as TAttribute;
if (attr==null) {
throw new MissingMemberException(typeof(T).Name+"."+propertyInfo.Name,typeof(TAttribute).Name);
}
return valueSelector(attr);
}
}
Run Code Online (Sandbox Code Playgroud)
使用静态方法...
var length = AttributeHelpers.GetMaxLength<Player>(x => x.PlayerName);
Run Code Online (Sandbox Code Playgroud)
或者在实例上使用可选的扩展方法......
var player = new Player();
var length = player.GetMaxLength(x => x.PlayerName);
Run Code Online (Sandbox Code Playgroud)
或者对任何其他属性使用完整的静态方法(例如 StringLength)...
var length = AttributeHelpers.GetPropertyAttributeValue<Player,string,StringLengthAttribute,Int32>(prop => prop.PlayerName,attr => attr.MaximumLength);
Run Code Online (Sandbox Code Playgroud)
受到这里答案的启发... /sf/answers/2275094951/
| 归档时间: |
|
| 查看次数: |
40384 次 |
| 最近记录: |