C# Reflection Basics
Reflection reads information about types at run time. It works through Type objects that describe classes, methods, and fields.
Getting a Type object
The typeof operator returns the Type for Sensor. The program prints its name and whether it is a class.
using System;
class Sensor
{
}
class Program
{
static void Main(string[] args)
{
Type type = typeof(Sensor);
Console.WriteLine(type.Name);
Console.WriteLine(type.IsClass);
}
}Listing method names
GetMethods returns the public instance methods declared on Sensor. The names are sorted before printing.
using System;
using System.Collections.Generic;
using System.Reflection;
class Sensor
{
public void Start()
{
}
public string Read()
{
return "data";
}
public void Stop()
{
}
}
class Program
{
static void Main(string[] args)
{
Type type = typeof(Sensor);
MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
List<string> names = new List<string>();
foreach (MethodInfo method in methods)
{
names.Add(method.Name);
}
names.Sort();
foreach (string name in names)
{
Console.WriteLine(name);
}
}
}Listing field names
GetFields returns the public fields of Sensor. The program prints the sorted names and then a count line.
using System;
using System.Collections.Generic;
using System.Reflection;
class Sensor
{
public string Name;
public int Level;
public string Code;
}
class Program
{
static void Main(string[] args)
{
Type type = typeof(Sensor);
FieldInfo[] fields = type.GetFields();
List<string> names = new List<string>();
foreach (FieldInfo field in fields)
{
names.Add(field.Name);
}
names.Sort();
foreach (string name in names)
{
Console.WriteLine(name);
}
Console.WriteLine("Count: " + names.Count);
}
}Example
using System;
using System.Collections.Generic;
using System.Reflection;
class Book
{
public string Title { get; set; }
public int Pages { get; set; }
public string Author { get; set; }
}
class Program
{
static void Main(string[] args)
{
Type type = typeof(Book);
Console.WriteLine(type.Name);
PropertyInfo[] props = type.GetProperties();
List<string> names = new List<string>();
foreach (PropertyInfo prop in props)
{
names.Add(prop.Name);
}
names.Sort();
foreach (string name in names)
{
Console.WriteLine(name);
}
}
}Key points
- A Type object describes one type in a running program.
- The typeof operator returns the Type object for a named type.
- GetMethods returns an array of MethodInfo items for a type.
- BindingFlags values control which members a search returns.
- DeclaredOnly limits a search to members declared on that type itself.
- The System.Reflection namespace holds Type and the member information types.
In this example, typeof returns the Type for Book. GetProperties returns its three properties. The names are sorted and printed under the class name.
Note
Reflection order is not fixed, so member listings are sorted by name before printing.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does typeof(Sensor) return?