Pass Data Between Application Domains - CSharp Custom Type

CSharp examples for Custom Type:AppDomain

Description

Pass Data Between Application Domains

Demo Code


using System;//from w  ww  .j a v  a2 s. c o  m
using System.Reflection;
using System.Collections;

    public class ListModifier
    {
        public ListModifier()
        {
            // Get the list from the data cache.
            ArrayList list = (ArrayList)AppDomain.CurrentDomain.GetData("Pets");

            // Modify the list.
            list.Add("turtle");
        }
    }

class MainClass
    {
        public static void Main()
        {
            AppDomain domain = AppDomain.CreateDomain("Test");

            // Create an ArrayList and populate with information.
            ArrayList list = new ArrayList();
            list.Add("dog");
            list.Add("cat");
            list.Add("fish");

            domain.SetData("Pets", list);

            domain.CreateInstance("Main","ListModifier");

            Console.WriteLine("Original list contents:");
            foreach (string s in list)
            {
                Console.WriteLine("  - " + s);
            }
            Console.WriteLine("\nModified list contents:");
            foreach (string s in (ArrayList)domain.GetData("Pets"))
            {
                Console.WriteLine("  - " + s);
            }
        }
    }

Result


Related Tutorials