Csharp/C Sharp by API/System.Reflection/Assembly
Содержание
- 1 Assembly.CodeBase
- 2 Assembly.EntryPoint
- 3 Assembly.EscapedCodeBase
- 4 Assembly.Evidence
- 5 Assembly.FullName
- 6 Assembly.GetCallingAssembly()
- 7 Assembly.GetCustomAttributes
- 8 Assembly.GetEntryAssembly()
- 9 Assembly.GetExecutingAssembly()
- 10 Assembly.GetExportedTypes
- 11 Assembly.GetName
- 12 Assembly.GetReferencedAssemblies
- 13 Assembly.GetTypes()
- 14 Assembly.GlobalAssemblyCache
- 15 Assembly.Load(AssemblyName name2)
- 16 Assembly.LoadFrom(String assemblyName)
- 17 Assembly.Load(String name)
- 18 Assembly.Location
Assembly.CodeBase
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);
}
}
Assembly.EntryPoint
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());
}
}
Assembly.EscapedCodeBase
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);
}
}
Assembly.Evidence
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());
}
}
}
Assembly.FullName
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);
}
}
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);
}
}
Assembly.GetCustomAttributes
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());
}
}
}
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);
}
}
Assembly.GetExecutingAssembly()
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() {
}
}
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");
}
}
}
Assembly.GetName
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;
}
}
Assembly.GetReferencedAssemblies
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());
}
}
Assembly.GetTypes()
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();
}
}
Assembly.GlobalAssemblyCache
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);
}
}
Assembly.Load(AssemblyName name2)
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);
}
}
Assembly.LoadFrom(String assemblyName)
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());
}
}
}
Assembly.Load(String name)
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);
}
}
Assembly.Location
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);
}
}