[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Use reflection to list the fields provided by the SystemInformation class in C#

[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. object property_value; FieldInfo[] field_infos = typeof(SystemInformation).GetFields( BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); 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"; if (info.IsFamilyAndAssembly) visibility += " family AND assembly"; if (info.IsFamilyOrAssembly) 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. if (!info.FieldType.IsArray) { // It's not an array. property_value = info.GetValue(this); if (property_value == null) value = "<null>"; else value = property_value.ToString(); } else { // 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>"; } else { foreach (Int32 val in values) value += ", " + val.ToString(); if (value.Length > 0) value = value.Substring(2); value = "[" + value + "]"; } } else { value = "<array>"; } } ListViewMakeRow(lvwProperties, name, info.FieldType.ToString(), 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.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.