Csharp/CSharp Tutorial/Assembly/AppDomain

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

An object in another application domain

using System;
using System.Runtime.Remoting;
using System.Reflection;
class MainClass
{
  public static void Main() 
  {
    AppDomain d = AppDomain.CreateDomain("NewDomain");
    
    ObjectHandle hobj = d.CreateInstance("AnotherDomain", "SimpleObject");
    SimpleObject so = (SimpleObject) hobj.Unwrap();
    Console.WriteLine(so.Display("make this uppercase"));
  }
}

[Serializable]
public class SimpleObject 
{
  public String Display(String inString)
  {
    return(inString.ToUpper());
  }
}

Create object using AppDomain.CreateInstanceAndUnwrap

using System;
using System.Runtime.Remoting;
class MainClass
{
  [MTAThread]
  static void Main(string[] args)
  {
    AppDomain Domain2 = AppDomain.CreateDomain("AppDomainB");
    MainClass MyMyClass = (MainClass)Domain2.CreateInstanceAndUnwrap("YourNameSpace", "YourClassName");
  }
}

Creation of an application domain

using System;
class MainClass
{
  public static void Main() 
  {
    AppDomain d = AppDomain.CreateDomain("NewDomain");
    Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
    Console.WriteLine(d.FriendlyName);
  }
}
main.exe
NewDomain

Get and set data to AppDomain

using System;
using System.Reflection;
using System.Collections;
class MainClass
{
    public static void Main() 
    {
        AppDomain domain = AppDomain.CreateDomain("Test");
        ArrayList list = new ArrayList();
        list.Add("d");
        list.Add("c");
        list.Add("f");
        domain.SetData("Pets", list);
        foreach (string s in (ArrayList)domain.GetData("Pets"))
        {
            Console.WriteLine("  - " + s);
        }
    }
}
- d
  - c
  - f

Get AppDomain Current Domain Friendly Name

using System;
using System.Text;
using System.Reflection;
class MainClass
{
    public static void Main ()
    {
        Console.WriteLine("Main AppDomain = {0}", AppDomain.CurrentDomain.FriendlyName);
    }
}
Main AppDomain = main.exe

Get current app domain name

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
            Console.WriteLine(AppDomain.CurrentDomain.GetAssemblies().Length.ToString());
            AppDomain ad = AppDomain.CreateDomain("MyDomain");
            ad.SetData("MYVALUE", "new Value");
            MyNameSpace.MyClass1 remoteType = (MyNameSpace.MyClass1)ad.CreateInstanceFromAndUnwrap("MyCode.dll", "MyNameSpace.MyClass1");
            remoteType.DoWork();
            AppDomain.Unload(ad);
        }
    }

Handle AppDomain Event: DomainUnload and ProcessExit

using System;
using System.Reflection;
using System.Windows.Forms;
  
public class MainClass
{
  public static void defaultAD_DomainUnload(object sender, EventArgs e)
  {
    Console.WriteLine("Unloaded defaultAD!");
  }
  private static void defaultAD_ProcessExit(object sender, EventArgs e)
  {
    Console.WriteLine("Unloaded defaultAD!");
  }
  public static int Main(string[] args)
  {
    AppDomain defaultAD = AppDomain.CreateDomain("SecondAppDomain");
    defaultAD.DomainUnload += new EventHandler(defaultAD_DomainUnload);
    defaultAD.ProcessExit +=new EventHandler(defaultAD_ProcessExit);
    // Now unload anotherAD.
    AppDomain.Unload(defaultAD);
    return 0;
  }
}
Unloaded defaultAD!

Make a new AppDomain in the current process

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;
  }
}
Here are the assemblies loaded in SecondAppDomain
-> Name: mscorlib
-> Version: 2.0.0.0

Marshal By Reference Object

using System;
public class MyClassByRef: MarshalByRefObject 
{
    public string HomeAppDomain
    {
        get
        {
            return AppDomain.CurrentDomain.FriendlyName;
        }
    }
}
public class MainClass
{
    public static void Main(string[] args)
    {
        AppDomain newDomain = AppDomain.CreateDomain("My New AppDomain");
        MyClassByRef mbrObject = 
            (MyClassByRef)newDomain.CreateInstanceFromAndUnwrap(
                "MainClass.exe", 
                "MyClassByRef");

        Console.WriteLine("AppDomain of MBR object = {0}", mbrObject.HomeAppDomain);
    }
}

marshall by value

using System;
[Serializable]
public class MyClass 
{
    public string HomeAppDomain 
    {
        get 
        {
            return AppDomain.CurrentDomain.FriendlyName;
        }
    }
}
public class MainClass
{
    public static void Main(string[] args)
    {
        AppDomain newDomain = AppDomain.CreateDomain("My New AppDomain");
        MyClass mbvObject = (MyClass)newDomain.CreateInstanceFromAndUnwrap(
                "MainClass.exe", 
                "MyClass");
        Console.WriteLine("AppDomain of MBV object = {0}", mbvObject.HomeAppDomain);

    }
}

Print All Assemblies In AppDomain

using System;
using System.Reflection;
using System.Windows.Forms;
  
public class MainClass
{
  public static int Main(string[] args)
  {
    AppDomain defaultAD = AppDomain.CurrentDomain;
    Console.WriteLine("This call loaded System.Windows.Forms.dll and System.dll");
    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;
  }
}
This call loaded System.Windows.Forms.dll and System.dll
Here are the assemblies loaded in main.exe
-> Name: mscorlib
-> Version: 2.0.0.0
-> Name: main
-> Version: 0.0.0.0

Unloading an application domain

using System;
using System.Runtime.Remoting;
using System.Reflection;
class MainClass
{
  public static void Main() 
  {
    AppDomain d = AppDomain.CreateDomain("NewDomain");
    
    ObjectHandle hobj = d.CreateInstance("AnotherDomain", "SimpleObject");
    SimpleObject so = (SimpleObject) hobj.Unwrap();
    Console.WriteLine(so.Display("make this uppercase"));
    AppDomain.Unload(d);
    Console.WriteLine(so.Display("make this uppercase"));
  }
}

[Serializable]
public class SimpleObject 
{
  public String Display(String inString)
  {
    return(inString.ToUpper());
  }
}

Use AppDomain to execute Assembly

using System;
class MainClass
{
    public static void Main(string[] args)
    {
        if (AppDomain.CurrentDomain.FriendlyName != "NewAppDomain")
        {
            AppDomain domain = AppDomain.CreateDomain("NewAppDomain");
            domain.ExecuteAssembly("MainClass.exe", null, args);
        }
        foreach (string s in args)
        {
            Console.WriteLine(AppDomain.CurrentDomain.FriendlyName + " : " + s);
        }
    }
}