Csharp/CSharp Tutorial/Assembly/Assembly Properties — различия между версиями

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

Текущая версия на 12:17, 26 мая 2010

Add information for AssemblyCulture and AssemblyVersion

using System;
using System.Reflection;
[assembly:AssemblyCulture("")]
[assembly:AssemblyVersion("1.1.0.5")]

class MainClass
{
    public static void Main() 
    {
        Console.WriteLine("Welcome");
    }
}
Welcome

Assembly.GetCallingAssembly()

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
public class MainClass
{
    public static void Main()
    {
        Assembly a3 = Assembly.GetCallingAssembly();
        Console.WriteLine(a3.FullName);
        
    }
}
main, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

Assembly.GetCallingAssembly() in a Thread

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
public class MainClass
{
    public static void Main()
    {
        ThreadPool.QueueUserWorkItem(delegate {
            Console.WriteLine(Assembly.GetCallingAssembly().FullName);
        });
        Thread.Sleep(100);
    }
}
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Assembly.GetEntryAssembly()

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
public class MainClass
{
    public static void Main()
    {
        Assembly a2 = Assembly.GetEntryAssembly();
        Console.WriteLine(a2.FullName);
        
    }
}
main, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

Assembly.GetExportedTypes

using System;
using System.Reflection;
public class MainClass
{
  static void Main(string[] args)
  {
    Assembly XMLAssembly;
    Type[] XMLTypes;
    XMLAssembly = Assembly.Load("System.Xml, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
    XMLTypes = XMLAssembly.GetExportedTypes();
    foreach(Type XMLType in XMLTypes)
    {
      object NewObject;
      Console.Write(XMLType.ToString());
      NewObject = XMLAssembly.CreateInstance(XMLType.ToString());
      if(NewObject != null)
        Console.WriteLine(" - Creation successful");
      else
        Console.WriteLine(" - CREATION ERROR");
    }
  }
}

Get an external assembly

using System;
using System.Reflection;
class MainClass
{
  public static void Main() 
  {
    AssemblyName anm = AssemblyName.GetAssemblyName("c:\\winnt\\microsoft.net\\framework\\v1.0.3705\\mscorlib.dll");
    Console.WriteLine(anm.Version.ToString());
  }
}

Get Entry Assembly

using System;
using System.Reflection;
using System;
using System.Reflection;
public class MainClass
{
  static void Main(string[] args)
  {
    Assembly EntryAssembly;
    EntryAssembly = Assembly.GetEntryAssembly();
    if(EntryAssembly.EntryPoint == null)
      Console.WriteLine("The assembly has no entry point.");
    else
      Console.WriteLine(EntryAssembly.EntryPoint.ToString());
  }
}

Get Evidence from Assembly

using System;
using System.Reflection;
using System.Collections;
using System.Security.Policy;
public class MainClass {
    public static void Main(string[] args) {
        Assembly a = Assembly.LoadFrom(args[0]);
        Evidence e = a.Evidence;
        IEnumerator x = e.GetHostEnumerator();
        Console.WriteLine("HOST EVIDENCE COLLECTION:");
        while (x.MoveNext()) {
            Console.WriteLine(x.Current.ToString());
        }
    }
}

Get the version object for current assembly

using System;
using System.Reflection;
[assembly:AssemblyVersionAttribute("1.2.3.4")]
[assembly:AssemblyTitleAttribute("Example")]
class MainClass
{
  public static void Main() 
  {
    
    Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
    Console.WriteLine(v.ToString());
    Console.WriteLine(v.Major + "." + v.Minor + "." + v.Build + "." + v.Revision);
    
  }
}
1.2.3.4
1.2.3.4

List all current Assemblies

using System;
using System.Reflection;
using System.Globalization;
class MainClass
{
   public static void Main(){
        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
        foreach (Assembly a in assemblies)
        {
            Console.WriteLine(a.GetName());
        }
   }
}
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
main, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

Load the System.Data assembly using a fully qualified display name

using System;
using System.Reflection;
using System.Globalization;
class MainClass
{
    public static void Main()
    {
        string name1 = "System.Data,Version=2.0.0.0," + 
            "Culture=neutral,PublicKeyToken=b77a5c561934e089";
        Assembly a1 = Assembly.Load(name1);
    }
}