Note. This is the answer to the previous question .
I decorate the setter property with the attribute TestMaxStringLengththat is used in the method called from the installer for verification.
Currently, the property is as follows:
public string CompanyName
{
get
{
return this._CompanyName;
}
[TestMaxStringLength(50)]
set
{
this.ValidateProperty(value);
this._CompanyName = value;
}
}
But I would prefer it to look like this:
[TestMaxStringLength(50)]
public string CompanyName
{
get
{
return this._CompanyName;
}
set
{
this.ValidateProperty(value);
this._CompanyName = value;
}
}
The code for ValidatePropertythat is responsible for finding the setter attributes:
private void ValidateProperty(string value)
{
var attributes =
new StackTrace()
.GetFrame(1)
.GetMethod()
.GetCustomAttributes(typeof(TestMaxStringLength), true);
}
How can I change the code ValidatePropertyto search for property attributes instead of the set method?
source
share