Note: This is a follow-up to an answer on a previous question.
I'm decorating a property's setter with an Attribute called TestMaxStringLength
that's used in method called from the setter for validation.
The property currently looks like this:
public string CompanyName
{
get
{
return this._CompanyName;
}
[TestMaxStringLength(50)]
set
{
this.ValidateProperty(value);
this._CompanyName = value;
}
}
But I would rather it look like this:
[TestMaxStringLength(50)]
public string CompanyName
{
get
{
return this._CompanyName;
}
set
{
this.ValidateProperty(value);
this._CompanyName = value;
}
}
The code for ValidateProperty
that is responsible for looking up the attributes of the setter is:
private void ValidateProperty(string value)
{
var attributes =
new StackTrace()
.GetFrame(1)
.GetMethod()
.GetCustomAttributes(typeof(TestMaxStringLength), true);
//Use the attributes to check the length, throw an exception, etc.
}
How can I change the ValidateProperty
code to look for attributes on the property instead of the set method?
In the class that declares the method, you could search for the property that contains that setter. It's not performant, but neither is
StackTrace
.As far as I know, there's no way to get a PropertyInfo from a MethodInfo of one of its setters. Though, of course, you could use some string hacks, like using the name for the lookup, and such. I'm thinking something like:
Needless to say, though, that's not exactly performant.
Also, be careful with the StackTrace class - it's a performance hog, too, when used too often.
You could consider, as an alternative approach, delaying validation until later, thus removing the need to inspect the stack trace.
This example provides an attribute...
... a POCO with the attribute applied to a property...
... and a utility class stub that validates the object.