gpt4 book ai didi

c# - 我如何使用反射找到数据注释属性及其参数

转载 作者:太空狗 更新时间:2023-10-29 18:23:12 25 4
gpt4 key购买 nike

我有一些这样的数据注释属性:

[StringLength(20, MinimumLength = 5, ErrorMessage = "First name must be between 5 and 20 characters")]

如何使用反射查找数据注释属性及其参数?

谢谢

最佳答案

我假设你有这样的东西:

[StringLength(20, MinimumLength = 5, ErrorMessage = "First name must be between 5 and 20 characters")]
public string FirstName {get;set;}

从中获取属性和属性:

StringLengthAttribute strLenAttr = 
typeof(Person).GetProperty("FirstName").GetCustomAttributes(
typeof(StringLengthAttribute), false).Cast<StringLengthAttribute>().Single();


int maxLen = strLenAttribute.MaximumLength;

关于c# - 我如何使用反射找到数据注释属性及其参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7864662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com