i want to make some validations in a class, so i thought I could use attributes.
Like this:
public class someClass
{
[Lenght(200)]
public string someStr { get; set; }
}
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
sealed class Lenght : Attribute
{
public Lenght(int Lenght)
{
//some code
}
}
But I know that I can’t do it, because this is not how attributes work. And, if there is a way, it would use some kind of heavy reflection and kludges that I want to avoid.
The validation I want to do is like this:
public class someClass
{
public string someStr
{
get
{
return _someStr;
}
set
{
if (value.Length > 200)
{
throw new Exception("Max Length is 200!");
}
else _someStr = value;
}
}
private string _someStr { get; set; }
}
But I want to do it faster, without all this code. I want as fast as using an Attribute.
There is a way i could do it?
Normally you would not do stuff like this with an attribute, but it is possible, although not recommendable. Use at your own risk 🙂 (you will let loose hell if the property is not decorated with the
LengthAttribute🙂 )EDIT I’ve extended the example. Still a horrible solution, but it works.