0
public class Test
{
    public int a = 2;
    public static int b = 5;
    public struct C
    {
        public int d = 9;
        public static int e = 7;
    }
}

new Test().Dump();

The code above will dump the newly created object and list a as a property but won't list b or the nested static struct C or anything inside of it.
If I have alot of auto generated static properties how do I dump everything?

Joe
  • 11,147
  • 7
  • 49
  • 60

2 Answers2

1

Reflection works

typeof(Test)
.GetFields(BindingFlags.Static | BindingFlags.Public)
.Select(f => new { name = f.Name, value = f.GetValue(null)})
.Dump();

enter image description here

Rm558
  • 4,621
  • 3
  • 38
  • 43
0

The static instance variables are not part of the "new Test()" instance that you are creating. They are part of the static instance of of the Test class. You can read up on static classes and Static class members here.

You can see the static variables by using

(Test.b).Dump();
(Test.C.e).Dump();

Hope this helps.

Brent Stewart
  • 1,830
  • 14
  • 21
  • well even in your scenario i could just do `Test.b.Dump()` without the brackets but if i have over 10,000 automatically generated static properties and structs then it becomes a problem to dump each and every single one of them individually. – Joe Mar 29 '11 at 04:54
  • The Dump() method in LinqPad is a static extension method, and extension methods require an instance of the object, so there is no way to dump the properties of the static class. – Brent Stewart Mar 29 '11 at 05:01
  • well, if `Test.b.Dump()` is possible and b is a static property of a class (not an instance of any object) then i'm hoping there is a way to actually get all the static properties dumped in one go... – Joe Mar 29 '11 at 05:11
  • Yes, you can access the Extension method on the instance of the int, but you will not be able to get the extension method to work on a static class. See this post on extension methods on static classes - http://stackoverflow.com/questions/249222/can-i-add-extension-methods-to-an-existing-static-class – Brent Stewart Mar 29 '11 at 05:17