using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using DesiredContractLibrary;


namespace ConsoleClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //method 1 - worked fine
            //http://stackoverflow.com/questions/1803540/load-assembly-at-runtime-and-create-class-instance
            //dll must be in bin directory (or gac)

            /*Assembly MyDLL = Assembly.Load("DesiredClassLibrary"); // DesiredClassLibrary.dll is name of my dll
            Type MyClass = MyDLL.GetType("DesiredClassLibrary.DesiredClass"); // DesiredClass is my class
            DesiredContract client = (DesiredContract)Activator.CreateInstance(MyClass);*/

            //or

            //DesiredContract client = (DesiredContract)Activator.CreateInstance(Type.GetType("DesiredClassLibrary.DesiredClass, DesiredClassLibrary", true));


            //method 2 - didn't worked, I can't find out why.
            //http://stackoverflow.com/questions/88717/loading-dlls-into-a-separate-appdomain
            //type must be serializable
            //runtime error:
            /*
             * O tipo não foi resolvido para o associado 'DesiredClassLibrary.DesiredClass,DesiredClassLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
             * */

            //AppDomain domain = AppDomain.CreateDomain("MyDomain");
            //string pathToDll = @"C:\Users\vitor.rubio\Dropbox\projetos\Exemplos\C#\WebDependencyLab_4th_example_reflection\DesiredClassLibrary\bin\Debug\DesiredClassLibrary.dll"; //Full path to dll you want to load           
            //DesiredContract client = (DesiredContract)domain.CreateInstanceFromAndUnwrap(pathToDll, "DesiredClassLibrary.DesiredClass");           


            //method 3 - worked fine
            //http://stackoverflow.com/questions/1137781/c-sharp-correct-way-to-load-assembly-find-class-and-call-run-method/1138426#1138426
            /*var asm = Assembly.LoadFile(@"C:\Users\vitor.rubio\Dropbox\projetos\Exemplos\C#\WebDependencyLab_4th_example_reflection\DesiredClassLibrary\bin\Debug\DesiredClassLibrary.dll");
            var type = asm.GetType("DesiredClassLibrary.DesiredClass");
            var client = Activator.CreateInstance(type) as DesiredContract;
            if (client == null) throw new Exception("broke");*/

            //method 4 - didn't worked, same as method 2
            /*var domain = AppDomain.CreateDomain("NewDomainName");
            var client = domain.CreateInstanceFromAndUnwrap(@"C:\Users\vitor.rubio\Dropbox\projetos\Exemplos\C#\WebDependencyLab_4th_example_reflection\DesiredClassLibrary\bin\Debug\DesiredClassLibrary.dll", "DesiredClassLibrary.DesiredClass") as DesiredContract;
            if (client == null) throw new Exception("broke");*/
            

            var asm = Assembly.LoadFile(@"C:\Users\vitor.rubio\Dropbox\projetos\Exemplos\C#\WebDependencyLab_4th_example_reflection\DesiredClassLibrary\bin\Debug\DesiredClassLibrary.dll");
            var type = asm.GetType("DesiredClassLibrary.DesiredClass");
            var client = Activator.CreateInstance(type) as DesiredContract;
            if (client == null) throw new Exception("broke");

            client.Name = "vitor";
            Console.WriteLine(client.GetHello());
            Console.ReadLine();
        }
    }
}
