Csharp/C Sharp by API/System.Reflection/Assembly

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

Assembly.CodeBase

<source lang="csharp"> using System; using System.Reflection; public class MainClass {

   static void Main() {
       Assembly EntryAssembly;
       EntryAssembly = Assembly.GetEntryAssembly();
       Console.WriteLine("Location: {0}", EntryAssembly.Location);
       Console.WriteLine("Code Base: {0}", EntryAssembly.CodeBase);
       Console.WriteLine("Escaped Code Base: {0}", EntryAssembly.EscapedCodeBase);
       Console.WriteLine("Loaded from GAC: {0}", EntryAssembly.GlobalAssemblyCache);
   }

}

 </source>


Assembly.EntryPoint

<source lang="csharp"> 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());
 }

}


 </source>


Assembly.EscapedCodeBase

<source lang="csharp"> using System; using System.Reflection; public class MainClass {

   static void Main() {
       Assembly EntryAssembly;
       EntryAssembly = Assembly.GetEntryAssembly();
       Console.WriteLine("Location: {0}", EntryAssembly.Location);
       Console.WriteLine("Code Base: {0}", EntryAssembly.CodeBase);
       Console.WriteLine("Escaped Code Base: {0}", EntryAssembly.EscapedCodeBase);
       Console.WriteLine("Loaded from GAC: {0}", EntryAssembly.GlobalAssemblyCache);
   }

}

 </source>


Assembly.Evidence

<source lang="csharp"> 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());
       }
   }

}


 </source>


Assembly.FullName

<source lang="csharp">


using System; using System.Reflection; class Starter {

   static void Main() {
       Assembly library = Assembly.Load("library, Version=2.0.0.0, " +
         "Culture=Neutral, PublicKeyToken=9b184fc90fb9648d");
       Console.WriteLine("Assembly.Load:  {0}", library.FullName);
       library = Assembly.LoadFrom("library.dll");
       Console.WriteLine("Assembly.LoadFrom {0}", library.FullName);
   }

}



 </source>


Assembly.GetCallingAssembly()

<source lang="csharp">

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);
       
   }

}


 </source>


Assembly.GetCustomAttributes

<source lang="csharp"> using System; using System.Reflection; class Program {

   static void Main(string[] args) {
       string assemblyName = "Your";
       try {
           Assembly a = Assembly.LoadFrom(assemblyName);
           object[] attributes = a.GetCustomAttributes(true);
           if (attributes.Length > 0) {
               Console.WriteLine("Assembly attributes for "{0}"...", assemblyName);
               foreach (object o in attributes)
                   Console.WriteLine("  {0}", o.ToString());
           } else
               Console.WriteLine("Assembly {0} contains no Attributes.", assemblyName);
       } catch (Exception ex) {
           Console.WriteLine(ex.ToString());
       }
   }

}


 </source>


Assembly.GetEntryAssembly()

<source lang="csharp">

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);
       
   }

}


 </source>


Assembly.GetExecutingAssembly()

<source lang="csharp">


using System; using System.IO; using System.Reflection; using System.Collections.Generic; public class Loader {

   static Dictionary<string, Assembly> libs = new Dictionary<string, Assembly>();
   static void Main() {
       AppDomain.CurrentDomain.AssemblyResolve += FindAssem;
       Program.Go();
   }
   static Assembly FindAssem(object sender, ResolveEventArgs args) {
       string shortName = new AssemblyName(args.Name).Name;
       if (libs.ContainsKey(shortName)) return libs[shortName];
       using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("Libs." + shortName + ".dll")) {
           byte[] data = new BinaryReader(s).ReadBytes((int)s.Length);
           Assembly a = Assembly.Load(data);
           libs[shortName] = a;
           return a;
       }
   }

} public class Program {

   public static void Go() {
   }

}


 </source>


Assembly.GetExportedTypes

<source lang="csharp">

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");
   }
 }

}


 </source>


Assembly.GetName

<source lang="csharp"> using System; using System.Reflection; using System.Windows.Forms;

public class MainClass {

 public static int Main(string[] args)
 {
   AppDomain defaultAD = AppDomain.CreateDomain("SecondAppDomain");
   Assembly[] loadedAssemblies = defaultAD.GetAssemblies();      
   Console.WriteLine("Here are the assemblies loaded in {0}\n",defaultAD.FriendlyName);
   foreach(Assembly a in loadedAssemblies)
   {
     Console.WriteLine("-> Name: {0}", a.GetName().Name);
     Console.WriteLine("-> Version: {0}\n", a.GetName().Version);
   }
   return 0;
 }

}


 </source>


Assembly.GetReferencedAssemblies

<source lang="csharp"> using System; using System.Reflection; public class MainClass {

   static void Main()
   {
   Assembly EntryAssembly;
   EntryAssembly = Assembly.GetEntryAssembly();
   foreach(AssemblyName Name in EntryAssembly.GetReferencedAssemblies())
     Console.WriteLine("Name: {0}", Name.ToString());
   }

}

 </source>


Assembly.GetTypes()

<source lang="csharp">

using System;

class MyClass {

 int x; 
 int y; 

 public MyClass(int i) { 
   Console.WriteLine("Constructing MyClass(int). "); 
   x = y = i;  
   show(); 
 } 

 public MyClass(int i, int j) { 
   Console.WriteLine("Constructing MyClass(int, int). "); 
   x = i; 
   y = j; 
   show(); 
 } 

 public int sum() { 
   return x+y; 
 } 

 public bool isBetween(int i) { 
   if((x < i) && (i < y)) return true; 
   else return false; 
 } 

 public void set(int a, int b) { 
   Console.Write("Inside set(int, int). "); 
   x = a; 
   y = b; 
   show(); 
 } 

 // Overload set. 
 public void set(double a, double b) { 
   Console.Write("Inside set(double, double). "); 
   x = (int) a; 
   y = (int) b; 
   show(); 
 } 

 public void show() { 
   Console.WriteLine("Values are x: {0}, y: {1}", x, y); 
 } 

}

class AnotherClass {

 string remark; 

 public AnotherClass(string str) { 
   remark = str; 
 } 

 public void show() { 
   Console.WriteLine(remark); 
 } 

}

///////////////////////////////////// using System; using System.Reflection;

class MainClass {

 public static void Main() { 
   int val; 

   Assembly asm = Assembly.LoadFrom("MyClasses.exe"); 

   Type[] alltypes = asm.GetTypes(); 
   foreach(Type temp in alltypes) 
     Console.WriteLine("Found: " + temp.Name); 

   Console.WriteLine(); 

 } 

}


 </source>


Assembly.GlobalAssemblyCache

<source lang="csharp"> using System; using System.Reflection; public class MainClass {

   static void Main() {
       Assembly EntryAssembly;
       EntryAssembly = Assembly.GetEntryAssembly();
       Console.WriteLine("Location: {0}", EntryAssembly.Location);
       Console.WriteLine("Code Base: {0}", EntryAssembly.CodeBase);
       Console.WriteLine("Escaped Code Base: {0}", EntryAssembly.EscapedCodeBase);
       Console.WriteLine("Loaded from GAC: {0}", EntryAssembly.GlobalAssemblyCache);
   }

}

 </source>


Assembly.Load(AssemblyName name2)

<source lang="csharp"> using System; using System.Reflection; using System.Globalization; class MainClass {

   public static void Main()
   {
       AssemblyName name2 = new AssemblyName();
       name2.Name = "System.Xml";
       name2.Version = new Version(2, 0, 0, 0);
       name2.CultureInfo = new CultureInfo("");    // Neutral culture.
       name2.SetPublicKeyToken( new byte[] {0xb7, 0x7a, 0x5c, 0x56, 0x19, 0x34, 0xe0, 0x89});
       Assembly a2 = Assembly.Load(name2);
   }

}


 </source>


Assembly.LoadFrom(String assemblyName)

<source lang="csharp"> using System; using System.Reflection; class Program {

   static void Main(string[] args) {
       string assemblyName = "Your";
       try {
           Assembly a = Assembly.LoadFrom(assemblyName);
           object[] attributes = a.GetCustomAttributes(true);
           if (attributes.Length > 0) {
               Console.WriteLine("Assembly attributes for "{0}"...", assemblyName);
               foreach (object o in attributes)
                   Console.WriteLine("  {0}", o.ToString());
           } else
               Console.WriteLine("Assembly {0} contains no Attributes.", assemblyName);
       } catch (Exception ex) {
           Console.WriteLine(ex.ToString());
       }
   }

}


 </source>


Assembly.Load(String name)

<source lang="csharp"> 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()
   {
       int x = 10;
       Assembly a = Assembly.Load("mscorlib");
       Type t3 = a.GetType("System.Int32");
       
       Type t1 = typeof(int);
       Type t2 = x.GetType();
       Type t4 = Type.GetType("System.Int32");
       Console.WriteLine(t1 == t2);
       Console.WriteLine(t2 == t3);
       Console.WriteLine(t3 == t4);
   }

}


 </source>


Assembly.Location

<source lang="csharp">


using System; using System.Reflection; using System.IO; public class AssemblyLoader {

 private Assembly LoadedAssembly;
 public AssemblyLoader(string LoadedAssemblyName, bool PartialName)
 {
     Console.WriteLine(LoadedAssemblyName);
   if(PartialName == true)
     LoadedAssembly = Assembly.LoadWithPartialName(LoadedAssemblyName);
   else
     LoadedAssembly = Assembly.Load(LoadedAssemblyName);
   WritePropertiesToConsole();
 }
 private void WritePropertiesToConsole()
 {
   Console.WriteLine("Full Name: {0}", LoadedAssembly.FullName);
   Console.WriteLine("Location: {0}", LoadedAssembly.Location);
   Console.WriteLine("Code Base: {0}", LoadedAssembly.CodeBase);
   Console.WriteLine("Escaped Code Base: {0}", LoadedAssembly.EscapedCodeBase);
   Console.WriteLine("Loaded from GAC: {0}", LoadedAssembly.GlobalAssemblyCache);
 }

} public class MainClass {

 static void Main(string[] args)
 {
   AssemblyLoader Loader;
   Loader = new AssemblyLoader("System.Xml, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false);
   Loader = new AssemblyLoader("System.Xml", false);
   Loader = new AssemblyLoader("System.Xml", true);
 }

}


 </source>