扩展方式:
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();}
码:
var name = player.GetAttributeFrom<DisplayAttribute>(nameof(player.PlayerDescription)).Name;var maxLength = player.GetAttributeFrom<MaxLengthAttribute>(nameof(player.PlayerName)).Length;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)