We have an asp.net MVC web application which uses the HttpRuntime.Cache
object to keep some static lookup values. We want to be able to monitor what\'s
being cached
Well, I think what you are asking for is a way of determining what the type parameters of a generic type is at runtime - in your example the situation is complicated because you are after an interface not an object instance.
Nethertheless this is still pretty straightforward, the following example should at least point you in the right direction on how to do this:
object obj= new List();
Type type = obj.GetType();
Type enumerable = type.GetInterfaces().FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>));
if (enumerable != null)
{
Type listType = enumerable.GetGenericArguments()[0];
if (listType == typeof(string))
{
IEnumerable e = obj as IEnumerable;
}
}
But I can't really see how this helps you solve your underlying problem of monitoring your cache?
In the past when attempting to monitor the performance of caches I've found creating my own simple Perfmon counters very helpful for monitor purposes - as a basic example start with a "# Entries" counter (which you increment whenever an item is added to the cache and decrement whenever an item is removed from the cache), and add counters as that you think would be useful as you go - a cache hit counter and a cache miss counter are normally pretty useful too.
You can also have your Perfmon counter break down caching information by having many instances of your counters, one for each type being cached (or in your case more likely the generic IEnumerable
type being cached) - just as the "Process" perfmon counter group has an instance for each process on your system, you would have an instance for each type in the cache (plus you should also add a "_Total" instance or similar).
Using Perfmon counters by recording operations on the cache allows you to monitor your cache performance in a fair amount of detail with very little runtime performance overhead.