Using reflection to get information about an assembly : Reflection Assembly « Development Class « C# / C Sharp






Using reflection to get information about an assembly

/*
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 ("\r\nMethods in {0}:", t.Name);
            MethodInfo [] methods = t.GetMethods ();
            foreach (MethodInfo m in methods)
                Console.WriteLine ("\t" + m.Name);
            Console.WriteLine ("\r\nProperties in {0}:", t.Name);
            PropertyInfo [] props = t.GetProperties ();
            foreach (PropertyInfo p in props)
                Console.WriteLine ("\t" + p.Name);
            Console.WriteLine ("\r\nFields in {0}:", t.Name);
            FieldInfo [] fields = t.GetFields ();
            foreach (FieldInfo f in fields)
                Console.WriteLine ("\t" + f.Name);
    }
}


           
       








Related examples in the same category

1.Get Method ParamemtersGet Method Paramemters
2.Get type infomation: base type, is abstract, is com object, is sealed, is classGet type infomation: base type, is abstract, is com object, is sealed, is class
3.Get all methods from a classGet all methods from a class
4.Get all fields from a classGet all fields from a class
5.Get all properties from a classGet all properties from a class
6.Get all implemented interface from a classGet all implemented interface from a class
7.Demonstrate using Type class to discover information about a classDemonstrate using Type class to discover information about a class
8.Demonstrate dynamically invoking an object
9.Locate an assembly, determine types, and create an object using reflection
10.Utilize MyClass without assuming any prior knowledge
11.Uses reflection to show the inherited members of a classUses reflection to show the inherited members of a class
12.Uses reflection to execute a class method indirectlyUses reflection to execute a class method indirectly
13.Deeper Reflection: Invoking Functions
14.Illustrates runtime type invocation
15.Illustrates unloading an application domain
16.Demonstrate typeofDemonstrate typeof
17.Create an object using reflectionCreate an object using reflection
18.Uses an object in another application domain
19.Illustrates creation of an application domainIllustrates creation of an application domain