Working with an Assembly Entry Point


   
 

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


Load assembly from namespace System.Xml

   
  


using System;
using System.Reflection;
using System.Globalization;

    class MainClass
    {
        public static void ListAssemblies()
        {
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); 
            foreach (Assembly a in assemblies)
            {
                Console.WriteLine(a.GetName());
            }
        }

        public static void Main()
        {
            ListAssemblies();
            string name1 = "System.Data, Version=2.0.0.0," +"Culture=neutral, PublicKeyToken=b77a5c561934e089";
            Assembly a1 = Assembly.Load(name1);

            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 a3 = Assembly.Load("SomeAssembly");

            Assembly a4 = Assembly.LoadFrom(@"c:sharedMySharedAssembly.dll");

            Console.WriteLine("

**** AFTER ****");
            ListAssemblies();

        }
    }

   
     


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

   
     


Location of Assembly

   
  

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

   
     


Set AssemblyTitle, AssemblyDescription, AssemblyConfiguration, AssemblyCompany, AssemblyProduct

   
  

using System.Reflection;
using System.Windows.Forms;
   
[assembly: AssemblyTitle("title")]
[assembly: AssemblyDescription("code.")]
[assembly: AssemblyConfiguration("Retail")]
[assembly: AssemblyCompany("Inc.")]
[assembly: AssemblyProduct("C#")]
[assembly: AssemblyCopyright("Inc.")]
[assembly: AssemblyVersion("1.0.*")]
   
public class SimpleHelloWorld : Form
{
    public static void Main()
    {
        Application.Run(new SimpleHelloWorld());
    }
   
    public SimpleHelloWorld()
    {
        Text = "Hello, WindowsForms!";
    }
}

   
     


Dynamically invoking methods from classes in an assembly.

   
  


using System;
using System.Reflection;

interface IMyInterface {
    void PrintAString(string s);
    void PrintAnInteger(int i);
    void PrintSomeNumbers(string desc, int i, double d);
    int GetANumber(string s);
}

public class MyClass : IMyInterface {
    public MyClass() {
    }
    public void PrintAString(string s) {
        Console.WriteLine("PrintAString: {0}", s);
    }
    public void PrintAnInteger(int i) {
        Console.WriteLine("PrintAnInteger: {0}", i);
    }
    public void PrintSomeNumbers(string desc, int i, double d) {
        Console.WriteLine("PrintSomeNumbers String: {0}", desc);
        Console.WriteLine("Integer: {0}", i);
        Console.WriteLine("Double: {0}", d);
    }
    public int GetANumber(string s) {
        Console.WriteLine("GetANumber: {0}", s);
        return 99;
    }
    public int DoItAll(string s, int i, double d) {
        IMyInterface mi = (IMyInterface)this;
        mi.PrintSomeNumbers(s, i, d);
        return mi.GetANumber(s);
    }
}

public class MainClass {
    public static void DoDynamicInvocation(string assembly) {
        Assembly a = Assembly.LoadFrom(assembly);
        foreach (Type t in a.GetTypes()) {
            if (t.IsClass == false)
                continue;
            if (t.GetInterface("IMyInterface") == null)
                continue;
            Console.WriteLine("Creating instance of class {0}", t.FullName);
            object obj = Activator.CreateInstance(t);
            object[] args = { "Dynamic", 1, 99.6 };
            object result;
            result = t.InvokeMember("DoItAll",BindingFlags.Default|BindingFlags.InvokeMethod,null,obj,args);
            Console.WriteLine("Result of dynamic call: {0}", result);
            object[] args2 = { 12 };
            t.InvokeMember("PrintAnInteger",BindingFlags.Default | BindingFlags.InvokeMethod,null,obj,args2);
        }
    }
    public static void Main(string[] args) {
        MyClass dmi = new MyClass();
        dmi.PrintSomeNumbers("PrintMe", 1, 1.9);
        int i = dmi.GetANumber("GiveMeOne");
        Console.WriteLine("I = {0}", i);

        DoDynamicInvocation(args[0]);
    }
}