Title: Use reflection to list the fields provided by the SystemInformation class in C#
The example Use reflection to list a class's fields in C# shows how to list the fields defined by a class. This example uses the techniques described in that example to list the few fields defined by the SystemInformation class.
When the program's form loads, the following code displays information about the class's properties.
// List the fields.
FieldInfo field_infos = typeof(SystemInformation).GetFields(
foreach (FieldInfo info in field_infos)
string name = info.Name;
string attributes = info.FieldType.Attributes.ToString();
string visibility = "";
if (info.IsAssembly) visibility += " assembly";
if (info.IsFamily) visibility += " family";
visibility += " family AND assembly";
visibility += " family OR assembly";
if (info.IsInitOnly) visibility += " init";
if (info.IsLiteral) visibility += " literal";
if (info.IsPrivate) visibility += " private";
if (info.IsPublic) visibility += " public";
if (info.IsStatic) visibility += " static";
if (visibility.Length > 0)
visibility = visibility.Substring(1);
string value = "";
// See if it's an array.
// It's not an array.
property_value = info.GetValue(this);
if (property_value == null)
value = "<null>";
value = property_value.ToString();
// It is an array.
name += "";
// If it's an array of integers, get the values.
if (info.FieldType.Name == "Int32")
Int32 values = (Int32)info.GetValue(this);
if (values == null)
value = "<null>";
foreach (Int32 val in values)
value += ", " + val.ToString();
if (value.Length > 0) value = value.Substring(2);
value = "[" + value + "]";
value = "<array>";
attributes, visibility, value);
The code gets the SystemInformation type and calls its GetFields method to get information about the class's fields. See the example Use reflection to list a class's properties in C# for information about the BindingFlags parameters.
The code then loops through the FieldInfo objects that GetFields returns. The code uses the FieldInfo object's Name property to get the field's name. It uses the object's FieldType.Attributes property to get information about the field's attributes.
Next, the code checks a series of FieldInfo properties to get information about the field's accessibility and build a string describing the values.
The code then uses methods similar to those used by the earlier example to display the field's value. This example also shows how to show the values in an integer array. If the field's type name is Int32 and the code can get its value, then the program loops through the field's value (which is an array) and adds its entries to a string.
The code finishes by calling the ListViewMakeRow method to display the information in the form's ListView control. Download the example program to see how that works.
Download the example to experiment with it and to see additional details.