Reflecting Assemblies

You can dynamically reflect an assembly by calling GetType or GetTypes on an Assembly object.

The following retrieves from the current assembly, the type called TestProgram in the Demos namespace:


using System;
using System.Reflection;
using System.Collections.Generic;
class Program
{
    static void Main()
    {
        Type t = Assembly.GetExecutingAssembly().GetType("Demos.TestProgram");

    }
}

The next example lists all the types in the assembly mylib.dll in e:\demo:


using System;
using System.Reflection;
using System.Collections.Generic;
class Program
{
    static void Main()
    {
        Assembly a = Assembly.LoadFrom(@"e:\demo\mylib.dll");

        foreach (Type t in a.GetTypes()) {
            Console.WriteLine(t);
        }    
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.