Using reflection to get information about an assembly

image_pdfimage_print
   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

// Asy.cs -- Demonstrates using reflection to get information
//           about an assembly.
//
//           Compile this program with the following command line:
//               C:>csc Asy.cs
using System;
using System.Reflection;

public class Asy
{
    static public void Main ()
    {
        Assembly asy = null;
        try
        {
            asy = Assembly.Load ("Circle");
        }
        catch (Exception e)
        {
            Console.WriteLine (e.Message);
            return;
        }
        Type [] types = asy.GetTypes();
        foreach (Type t in types)
            ShowTypeInfo (t);
    }
    static void ShowTypeInfo (Type t)
    {
            Console.WriteLine ("{0} is a member of the {1} namespace", t.Name, t.Namespace);
            Console.WriteLine ("
Methods in {0}:", t.Name);
            MethodInfo [] methods = t.GetMethods ();
            foreach (MethodInfo m in methods)
                Console.WriteLine ("	" + m.Name);
            Console.WriteLine ("
Properties in {0}:", t.Name);
            PropertyInfo [] props = t.GetProperties ();
            foreach (PropertyInfo p in props)
                Console.WriteLine ("	" + p.Name);
            Console.WriteLine ("
Fields in {0}:", t.Name);
            FieldInfo [] fields = t.GetFields ();
            foreach (FieldInfo f in fields)
                Console.WriteLine ("	" + f.Name);
    }
}