Csharp/C Sharp by API/System/AppDomain
Содержание
- 1 AppDomain.CreateDomain
- 2 AppDomain.CreateInstance
- 3 AppDomain.CreateInstanceAndUnwrap
- 4 AppDomain.CreateInstanceFromAndUnwrap
- 5 AppDomain.CurrentDomain
- 6 AppDomain.CurrentDomain.FriendlyName
- 7 AppDomain.DefineDynamicAssembly
- 8 AppDomain.DoCallBack
- 9 AppDomain.DomainUnload
- 10 AppDomain.FriendlyName
- 11 AppDomain.GetAssemblies
- 12 AppDomain.GetData
- 13 AppDomain.OnUnhandledException
- 14 AppDomain.ProcessExit
- 15 AppDomain.SetData
- 16 AppDomain.SetPrincipalPolicy
- 17 AppDomain.Unload
AppDomain.CreateDomain
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);
}
}
}
AppDomain.CreateInstance
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());
}
}
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");
}
}
AppDomain.CreateInstanceFromAndUnwrap
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);
}
}
AppDomain.CurrentDomain
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);
}
}
}
AppDomain.CurrentDomain.FriendlyName
using System;
class MainClass
{
public static void Main()
{
AppDomain d = AppDomain.CreateDomain("NewDomain");
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine(d.FriendlyName);
}
}
AppDomain.DefineDynamicAssembly
using System;
using System.Reflection;
using System.Reflection.Emit;
public class MainClass
{
public static void Main()
{
AppDomain ad = AppDomain.CurrentDomain;
AssemblyName an = new AssemblyName();
an.Name = "DynamicRandomAssembly";
AssemblyBuilder ab = ad.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
ModuleBuilder mb = ab.DefineDynamicModule("RandomModule");
TypeBuilder tb = mb.DefineType("DynamicRandomClass",TypeAttributes.Public);
Type returntype = typeof(int);
Type[] paramstype = new Type[0];
MethodBuilder methb=tb.DefineMethod("DynamicRandomMethod", MethodAttributes.Public, returntype, paramstype);
ILGenerator gen = methb.GetILGenerator();
gen.Emit(OpCodes.Ldc_I4, 1);
gen.Emit(OpCodes.Ret);
Type t = tb.CreateType();
Object o = Activator.CreateInstance(t);
Object[] aa = new Object[0];
MethodInfo m = t.GetMethod("DynamicRandomMethod");
int i = (int) m.Invoke(o, aa);
Console.WriteLine("Method {0} in Class {1} returned {2}",m, t, i);
}
}
using System;
public class DynamicRandomClass
{
public int DynamicRandomMethod()
{
return 1;
}
}
AppDomain.DoCallBack
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
public class MainClass
{
public static void Main()
{
List<AppDomain> ads = new List<AppDomain>();
for (int i = 0; i < 10; i++)
{
AppDomain ad = AppDomain.CreateDomain(i.ToString());
ad.DoCallBack(delegate { Type t = typeof(Uri); });
ads.Add(ad);
}
Console.WriteLine("After loading System.dll into 10 AppDomains: {0}", Environment.WorkingSet);
}
}
AppDomain.DomainUnload
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;
}
}
AppDomain.FriendlyName
using System;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
AppDomain currentDomain = AppDomain.CurrentDomain;
Console.WriteLine(currentDomain.FriendlyName);
AppDomain secondDomain = AppDomain.CreateDomain("New AppDomain");
secondDomain.CreateInstance("AssemblyA", "AppDomains.Demo", true,System.Reflection.BindingFlags.CreateInstance, null, new object[] { 7, 3 }, null, null, null);
}
}
AppDomain.GetAssemblies
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;
}
}
AppDomain.GetData
using System;
using System.Reflection;
using System.Collections;
class ListModifier{
public ListModifier()
{
ArrayList list = (ArrayList)AppDomain.CurrentDomain.GetData("Pets");
list.Add("turtle");
}
}
class MainClass{
public static void Main(){
AppDomain domain = AppDomain.CreateDomain("Test");
ArrayList list = new ArrayList();
list.Add("dog");
list.Add("cat");
list.Add("fish");
domain.SetData("Pets", list);
domain.CreateInstance("MainClass","Namespace.Namespace.ListModifier");
foreach (string s in (ArrayList)domain.GetData("Pets")) {
Console.WriteLine(s);
}
}
}
AppDomain.OnUnhandledException
using System;
public class Starter {
public static void Main() {
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
int vara = 5, varb = 0;
vara /= varb;
}
public static void OnUnhandledException(
object sender, UnhandledExceptionEventArgs e) {
string application_name = sender.ToString();
Exception except = (Exception)e.ExceptionObject;
string errormessage = "Application " + application_name +
" [ Exception " + except.Message + " ]";
Console.WriteLine(errormessage);
}
}
AppDomain.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;
}
}
AppDomain.SetData
using System;
using System.Reflection;
using System.Collections;
class ListModifier{
public ListModifier()
{
ArrayList list = (ArrayList)AppDomain.CurrentDomain.GetData("Pets");
list.Add("turtle");
}
}
class MainClass{
public static void Main(){
AppDomain domain = AppDomain.CreateDomain("Test");
ArrayList list = new ArrayList();
list.Add("dog");
list.Add("cat");
list.Add("fish");
domain.SetData("Pets", list);
domain.CreateInstance("MainClass","Apress.VisualCSharpRecipes.Chapter03.ListModifier");
foreach (string s in (ArrayList)domain.GetData("Pets")) {
Console.WriteLine(s);
}
}
}
AppDomain.SetPrincipalPolicy
using System;
using System.Security;
using System.Security.Principal;
using System.Security.Permissions;
class Program {
static void Main(string[] args) {
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
try {
ShowMessage();
} catch (SecurityException exception) {
Console.WriteLine(exception.Message);
}
}
[PrincipalPermissionAttribute(SecurityAction.Demand,Role = "BUILTIN\\Users")]
static void ShowMessage() {
Console.WriteLine("The current principal is logged in locally ");
Console.WriteLine("(they are a member of the local Users group)");
}
}
AppDomain.Unload
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());
}
}