0

Is it possible to retrieve all the static objects which have been created in the current appdomain? I'm trying to write a debug page for an existing asp.net web site. which holds a lot of information in a caches based on singleton objects.

thanks

2 Answers2

0

Here's something to get you started:

IList<Assembly> assemblies = AppDomain.CurrentDomain.GetAssemblies();

List<Type> allTypes = new List<Type>();

foreach (Assembly assembly in assemblies)
{
    var types = from t in assembly.GetTypes()
                where bootStrapperType.IsAssignableFrom(t)
                    && !t.IsInterface && !t.IsAbstract
                select t;

    allTypes.AddRange(types);
}

foreach(Type type in allTypes)
{
    var fieldsPublic = type.GetFields(BindingFlags.Public|BindingFlags.Static);
    foreach(var field in fieldsPublic)
    {
       var nameAndValue = field.Name + " = " + field.GetValue(null);
       // Do something
    }
    var fieldsPrivate = type.GetFields(BindingFlags.Private|BindingFlags.Static);
    foreach(var field in fieldsPrivate)
    {
       var nameAndValue = field.Name + " = " + field.GetValue(null);
       // Do something.
    }
}
Diego
  • 18,035
  • 5
  • 62
  • 66
  • thanks - that has pointed me in the correct direction. In order to remove constants from the results I also need to check for field.IsLiteral – David Betteridge Feb 25 '12 at 22:29
-1

I've come across this and this sometime ago. You need to know the types that contain those static fields though. But you can iterate over all loaded assembles like this and check every type.

Community
  • 1
  • 1
enamrik
  • 2,292
  • 2
  • 27
  • 42
  • Whilst this may theoretically answer the question (it doesn't, by the way), [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Grant Thomas Feb 25 '12 at 00:50