Csharp/CSharp Tutorial/Assembly/AppDomain
Содержание
- 1 An object in another application domain
- 2 Create object using AppDomain.CreateInstanceAndUnwrap
- 3 Creation of an application domain
- 4 Get and set data to AppDomain
- 5 Get AppDomain Current Domain Friendly Name
- 6 Get current app domain name
- 7 Handle AppDomain Event: DomainUnload and ProcessExit
- 8 Make a new AppDomain in the current process
- 9 Marshal By Reference Object
- 10 marshall by value
- 11 Print All Assemblies In AppDomain
- 12 Unloading an application domain
- 13 Use AppDomain to execute Assembly
An object in another application domain
<source lang="csharp">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()); }
}</source>
Create object using AppDomain.CreateInstanceAndUnwrap
<source lang="csharp">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"); }
}</source>
Creation of an application domain
<source lang="csharp">using System; class MainClass {
public static void Main() { AppDomain d = AppDomain.CreateDomain("NewDomain"); Console.WriteLine(AppDomain.CurrentDomain.FriendlyName); Console.WriteLine(d.FriendlyName); }
}</source>
main.exe NewDomain
Get and set data to AppDomain
<source lang="csharp">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); } }
}</source>
- d - c - f
Get AppDomain Current Domain Friendly Name
<source lang="csharp">using System; using System.Text; using System.Reflection; class MainClass {
public static void Main () { Console.WriteLine("Main AppDomain = {0}", AppDomain.CurrentDomain.FriendlyName); }
}</source>
Main AppDomain = main.exe
Get current app domain name
<source lang="csharp">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); } }</source>
Handle AppDomain Event: DomainUnload and ProcessExit
<source lang="csharp">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; }
}</source>
Unloaded defaultAD!
Make a new AppDomain in the current process
<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>
Here are the assemblies loaded in SecondAppDomain -> Name: mscorlib -> Version: 2.0.0.0
Marshal By Reference Object
<source lang="csharp">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); }
}</source>
marshall by value
<source lang="csharp">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);
}
}</source>
Print All Assemblies In AppDomain
<source lang="csharp">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; }
}</source>
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
<source lang="csharp">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()); }
}</source>
Use AppDomain to execute Assembly
<source lang="csharp">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); } }
}</source>