1

I am employing a logic similar to the suggestion present in the Microsoft documentation https://msdn.microsoft.com/en-us/library/cs58sb90(v=vs.110).aspx to retrieve the custom attributes given to the parameter of a method.

However, I find this approach expensive as it uses reflection to retrieve the custom attributes, I am wondering if there is any better approach to this problem than using reflection?

7
  • AFAIK, the only way to get attributes values is reflection. Commented May 1, 2018 at 4:46
  • I don't think that doing reflection is that very expensive. Are you looking at discovering attributes across million of class. Because that i think is expensive. You should read this article.blog.codinghorror.com/… Commented May 1, 2018 at 4:46
  • 'expensive' is a relative term. What is the purpose of your app? What is you implementation look like? Commented May 1, 2018 at 4:57
  • @DaniDev Please see the other detailed post on stakoverflow on the problem that I am trying to solve using this approach.stackoverflow.com/questions/50110535/… Commented May 1, 2018 at 5:24
  • 1
    Anyway just for your information, i did 1000 custom attribute lockups in 4 millisecond or on average 0.004 ms per lookup, averaged over 100 runs Commented May 1, 2018 at 6:13

1 Answer 1

1

The short answer as far as I am aware is no, reflection is the standard and probably only way to retrieve attributes.

However, attributes are fixed in the type metadata at compile-time, which means you will only ever have to inspect them once for each type during the lifetime of your program, and they won't change in that time unless your code is doing some really wacky runtime type construction (e.g. with Reflection.Emit). Even if you don't know the exact type of any object your code requires the attributes of, you could still cache the type's attributes in a dictionary to save looking them up again, if you're really that concerned about performance.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Tom - sounds like a solution to me , I will give it a go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.