TransparentProxies and ArrayLists

Sep 29 2009 2:44 PM
I have an AppDomain, I CreateInstanceAndUnwrap on this AppDomain on an object that inherits from MarshalByRefObject. The object that returns is a __TransparentProxy, it is then cast to the interface the underlying object implements. This object has an ArrayList property. I'm able to read this ArrayList just fine, list[5] returns a value. But all attempts to Add() items results in nothing happening. No exception and the item is not added.

Does anyone know why this happens?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace WebQuestion
{
    interface IListInterface
    {
        ArrayList Items { get; set; }
    }

    class ListClass : MarshalByRefObject, IListInterface
    {
        private ArrayList items = new ArrayList{0,1,2,3,4,5};
        public ArrayList Items
        {
            get
            {
                return items;
            }
            set
            {
                items = value;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            AppDomain ad = AppDomain.CreateDomain("OtherAppDomain");
            IListInterface byRef = (IListInterface)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "WebQuestion.ListClass");
            foreach (object obj in byRef.Items)
            {
                Console.WriteLine(obj);
            }

            Console.WriteLine("Item Count before Add(): " + byRef.Items.Count);  //Items.Count == 6
            byRef.Items.Add("Item at index 6");
            Console.WriteLine("Item Count after Add(): " + byRef.Items.Count);   //Items.Count == 6, still

            try
            {
                Console.WriteLine("Getting item at Items[6]...");
                Console.WriteLine(byRef.Items[6]);  //throw exception
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}


Answers (2)