Joe
Joe

Reputation: 11677

dumping static properties in linqpad

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?

Upvotes: 0

Views: 629

Answers (2)

Rm558
Rm558

Reputation: 5002

Reflection works

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

enter image description here

Upvotes: 1

Brent Stewart
Brent Stewart

Reputation: 1840

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.

Upvotes: 1

Related Questions