Alex Rivera | Logout

To cache or not to cache - GetCustomAttributes

Asked 2009-10-23T08:38:20.397
20

I currently have a function:

public static Attribute GetAttribute(MemberInfo Member, Type AttributeType)
{
    Object[] Attributes = Member.GetCustomAttributes(AttributeType, true);

    if (Attributes.Length > 0)
        return (Attribute)Attributes[0];
    else
        return null;
}

I am wondering if it would be worthwhile caching all the attributes on a property into a Attribute = _cache[MemberInfo][Type] dictionary,

This would require using GetCustomAttributes without any type parameter then enumerating over the result. Is it worth it?

Edit
Report

1 Answer

2

Are you actually having a performance problem? If not then don't do it until you need it.

It might help depending on how often you call the method with the same paramters. If you only call it once per MemberInfo, Type combination then it won't do any good. Even if you do cache it you are trading speed for memory consumption. That might be fine for your application.

answered 2009-10-23T08:46:28.930

Your Answer