1

C# 4.0. I have a slow property with an attribute. I want to read this attribute without calling the getter:

[Range(0.0f, 1000.0f)]
public float X
{
    get
    {
        return SlowFunctionX();
    }
}

This is what I have now:

public static T GetRangeMin<T>(T value)
{
    var attribute = value.GetType()
        .GetField(value.ToString())
        .GetCustomAttributes(typeof(RangeAttribute), false)
        .SingleOrDefault() as RangeAttribute;

    return (T)attribute.Minimum;
}

var min = GetRangeMin<double>(X); // Will call the getter of X :(

Q: How can I read this attribute without calling the getter of X?

l33t
  • 18,692
  • 16
  • 103
  • 180
  • This might help..similar question http://stackoverflow.com/questions/6637679/reflection-get-attribute-name-and-value-on-property/6637710#6637710 – Amitd Mar 25 '13 at 17:21

2 Answers2

9

To read the attribute on the property just load the attribute directly

var attrib = typeof(TheTypeContainingX)
  .GetProperty("X")
  .GetCustomAttributes(typeof(RangeAttribute), false)
  .Cast<RangeAttribute>()
  .FirstOrDefault();
return attrib.Minimum;
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

You won't be able to get it like that anyway, because you'll be calling something like GetRangeMin<float>(0.0f), and the float type doesn't have a field called whatever-value-X-has.

If you want to do it in a general and type-safe way, you need to use Expressions:

public static T GetRangeMin<T>(Expression<Func<T>> value)

invoked thus:

var min = GetRangeMin(() => X);

You then need to navigate the expression tree to get the property info

MemberExpression memberExpression = value.Body as MemberExpression;
if (null == memberExpression || memberExpression.Member.MemberType != MemberTypes.Property)
    throw new ArgumentException("Expect a field access", "FieldExpression");
PropertyInfo propInfo = (PropertyInfo)memberExpression.Member;

Now you can GetCustomAttributes on the propInfo. Just as an aside, if you're worried about inheritance, you might need to use Attribute.GetCustomAttributes(propInfo, ...) because propInfo.GetCustomAttributes(...) won't walk the inheritance tree even if you ask it to.

MattW
  • 4,480
  • 1
  • 12
  • 11