Reflecting on Fields and Properties
The implementation of ListFields() is similar. The only notable difference is the call to Type.
GetFields() and the resulting FieldInfo array. Again, to keep things simple, you are printing out
only the name of each field.
// Display field names of type.
static void ListFields(Type t)
{
Console.WriteLine("***** Fields *****");
FieldInfo[] fi = t.GetFields();
foreach(FieldInfo field in fi)
Console.WriteLine("->{0}", field.Name);
Console.WriteLine();
}
The logic to display a type’s properties is similar:
// Display property names of type.
static void ListProps(Type t)
{
Console.WriteLine("***** Properties *****");
PropertyInfo[] pi = t.GetProperties();
foreach(PropertyInfo prop in pi)
Console.WriteLine("->{0}", prop.Name);
Console.WriteLine();
}
Do'stlaringiz bilan baham: |