What is the most efficient way of associating information with a Type in .Net?

Posted by Miguel Angelo on Stack Overflow See other posts from Stack Overflow or by Miguel Angelo
Published on 2010-04-30T19:16:07Z Indexed on 2010/04/30 19:17 UTC
Read the original article Hit count: 199

Filed under:
|
|

I want to associate custom data to a Type, and retrieve that data in run-time, blasingly fast.

This is just my imagination, of my perfect world:

var myInfo = typeof(MyClass).GetMyInformation();

this would be very fast... of course this does not exist! If it did I would not be asking. hehe ;)

This is the way using custom attributes:

var myInfo = typeof(MyClass).GetCustomAttribute("MyInformation");

this is slow because it requires a lookup of the string "MyInformation"

This is a way using a Dictionary<Type, MyInformation>:

var myInfo = myInformationDictionary[typeof(MyClass)];

This is also slow because it is still a lookup of 'typeof(MyClass)'.

I know that dictionary is very fast, but this is not enough... it is not as fast as calling a method. It is not even the same order of speed.

I am not saying I want it to be as fast as a method call. I want to associate information with a type and access it as fast as possible. I am asking whether there is a better way, or event a best way of doing it.

Any ideas??

Thanks!

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#