Thursday, February 11, 2010

Running a class in a seperate AppDomain

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TestClass currentDomainTestClass = new TestClass();
            currentDomainTestClass.TestMethod("From current domain");

            AppDomain appDomain = AppDomain.CreateDomain("appDomain");
            TestClass otherDomainTestClass = (TestClass)appDomain.CreateInstanceAndUnwrap(
                typeof(TestClass).Assembly.FullName,
                typeof(TestClass).FullName);

            otherDomainTestClass.TestMethod("Seperate domain");

            Console.ReadLine();
        }
    }

    public class TestClass : MarshalByRefObject
    {
        public void TestMethod(string message)
        {
            Console.WriteLine("Host domain: {0} Message: {1}",
                AppDomain.CurrentDomain.FriendlyName,
                message);

        }
    }
}

MSDN: Using Application Domains