6

Why does the following code produce no output?

static void Main(string[] args)
{
    FieldInfo[] fi = typeof(MyStruct).GetFields(BindingFlags.Public);
    foreach (FieldInfo info in fi)
    {
        Console.WriteLine(info.Name);
    }
}

public struct MyStruct
{
    public int one;
    public int two;
    public int three;
    public int four;
    public int five;
    public int six;
    public bool seven;
    public String eight;
}
John Saunders
  • 159,224
  • 26
  • 237
  • 393
Odrade
  • 7,150
  • 11
  • 41
  • 65

1 Answers1

25

You need to or in the instance binding as well. Change your code to:

FieldInfo[] fi = typeof(MyStruct).GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo info in fi)
{
    Console.WriteLine(info.Name);
}
Jake Pearson
  • 25,953
  • 10
  • 72
  • 91