CHAPTER 15 TYPE REFLECTION, LATE BINDING, AND ATTRIBUTE-BASED PROGRAMMING
Displaying Various Odds and Ends
Last but not least, you have one final helper method that will simply display various statistics (indicating
whether the type is generic, what the base class is, whether the type is sealed, and so forth) regarding the
incoming type.
// Just for good measure.
static void ListVariousStats(Type t)
{
Console.WriteLine("***** Various Statistics *****");
Console.WriteLine("Base class is: {0}", t.BaseType);
Console.WriteLine("Is type abstract? {0}", t.IsAbstract);
Console.WriteLine("Is type sealed? {0}", t.IsSealed);
Console.WriteLine("Is type generic? {0}", t.IsGenericTypeDefinition);
Console.WriteLine("Is type a class type? {0}", t.IsClass);
Console.WriteLine();
}
Implementing Main()
The Main() method of the Program class prompts the user for the fully qualified name of a type. Once you
obtain this string data, you pass it into the Type.GetType() method and send the extracted System.Type
into each of your helper methods. This proce ss repeats until the user enters Q to terminate the
application.
static void Main(string[] args)
{
Console.WriteLine("***** Welcome to MyTypeViewer *****");
string typeName = "";
do
{
Console.WriteLine("\nEnter a type name to evaluate");
Console.Write("or enter Q to quit: ");
// Get name of type.
typeName = Console.ReadLine();
// Does user want to quit?
if (typeName.ToUpper() == "Q")
{
break;
}
// Try to display type.
try
{
Type t = Type.GetType(typeName);
Console.WriteLine("");
ListVariousStats(t);
ListFields(t);
ListProps(t);
566