0
如何查找属性的值?我需要检查该值并将文本框maxlength设置为该值。以下是我希望检索的值的示例。如何查找属性的值
public class DogClass
{
[StringLength(5)]
public string LegalName
{
}
如何查找属性的值?我需要检查该值并将文本框maxlength设置为该值。以下是我希望检索的值的示例。如何查找属性的值
public class DogClass
{
[StringLength(5)]
public string LegalName
{
}
您可以使用反射来获取此信息。下面是一个应该让你开始的片段。
protected void GetStringLength(object objDog) {
// loop through each property in object
foreach (PropertyInfo pi in objDog.GetType().GetProperties())
{
// for each object property, get the SringLength tag (if there is one)
foreach (Attribute attribute in Attribute.GetCustomAttributes(pi, typeof(StringLengthAttribute), true))
{
// we'll assume there is only one
var stringLenVal = (attribute as StringLengthAttribute).MaximumLength;
break;
}
}
}
完全想要我想要的感谢。 – user698625
你的意思是像return _LegalName? –
我需要返回5一些如何。所以我可以设置文本框的最大长度为5 – user698625