我寻找一种在属性中获取自己的属性的方法。
让我展示我正在搜索的内容
我想使用浮点/双精度值的属性来提供比较容差。
例如
[FieldAttribute(CompareTolerance = 0.001)]
public float SomeProperty
{
get { return this.someProperty; }
set
{
if (Math.Abs(someProperty- value) > 0.001) // here i would like to use somthing like '> FieldAttribute.CompareTolerance'
this.someProperty = value;
}
}
从另一个班级我会使用
PropertyInfo propertyInfo = someobject.GetType().GetProperty("SomeProperty");
if (null != propertyInfo)
{
Attribute attribute = Attribute.GetCustomAttribute(propertyInfo, typeof (FieldAttribute));
FieldAttribute fieldAttribute = attribue as FieldAttribute;
return fieldAttribute.CompareTolerance;
}
...
所以最后我只需要
if(Math.Abs(someProperty - value) < someobject.CompareTolerance("SomeField")) ... values are equal
但是有没有一种方法可以在不使用反射的情况下在属性中获取属性( this.CompareTolerance("SomeField") )