I have an object which has properties decorated with Validation attributes. My validation is working correctly, however I need an additional method which performs string manipulation based on those attributes.
Consider the following object:
public class Pupil
{
/// <summary>
///
/// </summary>
public Pupil()
{
}
[NotNullValidator]
[StringLengthValidator(0, 54, MessageTemplate = "Invalid value '{0}' for {1}, Max length: {5}")]
public string Name{ get; set; }
[NotNullValidator]
[StringLengthValidator(0, 2, MessageTemplate = "Invalid value '{0}' for {1}, Max length: {5}")]
public string Gender{ get; set; }
}
I want to be able to manipulate the “Name” based on the StringLengthValidator attribute and its arguments. For example:
///Takes a Pupil object in
public static void DoManipulation(object value)
{
foreach(var property in value.GetType().GetProperties())
{
if(property.Name == "Name")
{
var att = property.GetCustomAttributes(typeof(StringLengthValidator), false);
var length = ((StringLengthValidator)att[0]).UpperBound;
}
}
}
The value of “length” is coming up as null, rather than 54. How do I get the value out?
Hopefully this makes sense, thanks.
A
This works for me, are you getting the same
StringLengthValidatorattribute that you think you are? (is this your custom class or the one from Enterprise Lib?In my case, I created a custom class
StringLengthValidator