What is the most efficient way of associating information with a Type in .Net?
- by Miguel Angelo
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!