Csharp/CSharp Tutorial/Reflection/Assembly

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Load Assembly from Dll

<source lang="csharp">using System; using System.Reflection; using System.Resources; using System.Collections.Generic; using System.Text;

   namespace AssemblyDemo1
   {
       class Employee
       {
       }
   }
   class Program
   {
       static void Main(string[] args)
       {
           Assembly a = Assembly.GetExecutingAssembly();
           Console.WriteLine(a.GetName().Name, a.Location);
           Console.WriteLine(a.GlobalAssemblyCache);
           Console.WriteLine(a.ImageRuntimeVersion);
           Assembly otherAssembly = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "\\SampleAssembly.DLL");
           Console.WriteLine("Other Assembly: {0}", otherAssembly.GetName().Name);
           Console.WriteLine("Types contained in {0}", otherAssembly.GetName().Name);
           foreach (Type t in otherAssembly.GetTypes())
           {
               Console.WriteLine(t.Name);
           }
       }
   }</source>

Reflecting An Assembly

<source lang="csharp">using System; using System.Collections.Generic; using System.Reflection; using System.Text;

  public class Tester
  {
     public static void Main()
     {
        Assembly a = Assembly.Load( "Mscorlib.dll" );
        Type[] types = a.GetTypes();
        foreach ( Type t in types )
        {
           Console.WriteLine( "Type is {0}", t );
        }
        Console.WriteLine(
           "{0} types found", types.Length );
     }
  }</source>

Reflecting On A Type

<source lang="csharp">using System; using System.Collections.Generic; using System.Reflection; using System.Text;

   public class Tester
   {
       public static void Main()
       {
  
           Type theType = Type.GetType("System.Reflection.Assembly");
           Console.WriteLine("\nSingle Type is {0}\n", theType);
       }
   }</source>