Csharp/C Sharp/Development Class/AppDomain — различия между версиями

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

Текущая версия на 11:43, 26 мая 2010

AppDomain.CreateDomain

 

using System; 
    class MainClass
    {
        public static void Main()
        {
            AppDomainSetup setupInfo = new AppDomainSetup();
            setupInfo.ApplicationBase = @"C:\MyRootDirectory";
            setupInfo.ConfigurationFile = "MyApp.config";
            setupInfo.PrivateBinPath = "bin;plugins;external";
            AppDomain newDomain = AppDomain.CreateDomain("My New AppDomain", null, setupInfo);
        }
    }


Configure the AppDomainSetup

using System;
class Test
{
    public static void Main()
    {
        AppDomainSetup setupInfo = new AppDomainSetup();
        setupInfo.ApplicationBase = @"C:\MyRootDirectory";
        setupInfo.ConfigurationFile = "MyApp.config";
        setupInfo.PrivateBinPath = "bin;plugins;external";
        AppDomain newDomain = 
            AppDomain.CreateDomain("My New AppDomain", null, setupInfo);
    }
}


creates three instances of the same type. A local and two remote proxies to object instances are constructed:

 
using System;
using System.Reflection;
using System.Runtime.Remoting;

class MyClass {
    public void MethodA(DateTime dt) {
        Console.WriteLine("MethodA invoked at " + dt.ToLongTimeString());
    }
}
class Starter {
    static void Main() {
        CreateLocal();
        CreateRemote1();
        CreateRemote2();
    }
    static void CreateLocal() {
        object obj = Activator.CreateInstance(typeof(MyClass));
        ((MyClass)obj).MethodA(DateTime.Now);
    }
    static void CreateRemote1() {
        ObjectHandle hObj = Activator.CreateInstance("library","MyClass");
        object obj = hObj.Unwrap();
        MethodInfo method = obj.GetType().GetMethod("MethodA");
        method.Invoke(obj, new object[1] { DateTime.Now });
    }
    static void CreateRemote2() {
        AppDomain domain = AppDomain.CurrentDomain;
        object obj = domain.CreateInstanceFromAndUnwrap("library.dll","MyClass");
        MethodInfo method = obj.GetType().GetMethod("MethodA");
        method.Invoke(obj, new object[1] { DateTime.Now });
    }
}


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


OnUnhandledException method is added to the AppDomain.UnhandledException event

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


SetData, 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","Apress.VisualCSharpRecipes.Chapter03.ListModifier");
          foreach (string s in (ArrayList)domain.GetData("Pets")) {
              Console.WriteLine(s);
          }
      }
}


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