473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ArrayLists and TransparentProxies

3
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?
Sep 29 '09 #1
5 1666
smulz
3
Here's an example of what I mean...



Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7.  
  8. namespace WebQuestion
  9. {
  10.     interface IListInterface
  11.     {
  12.         ArrayList Items { get; set; }
  13.     }
  14.  
  15.     class ListClass : MarshalByRefObject, IListInterface
  16.     {
  17.         private ArrayList items = new ArrayList{0,1,2,3,4,5};
  18.         public ArrayList Items 
  19.         { 
  20.             get
  21.             {
  22.                 return items;
  23.             } 
  24.             set
  25.             {
  26.                 items = value;
  27.             } 
  28.         }
  29.     }
  30.  
  31.     class Program
  32.     {
  33.         static void Main(string[] args)
  34.         {
  35.             AppDomain ad = AppDomain.CreateDomain("OtherAppDomain");
  36.             IListInterface byRef = (IListInterface)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "WebQuestion.ListClass");
  37.             foreach (object obj in byRef.Items)
  38.             {
  39.                 Console.WriteLine(obj);
  40.             }
  41.  
  42.             Console.WriteLine("Item Count before Add(): " + byRef.Items.Count);  //Items.Count == 6
  43.             byRef.Items.Add("Item at index 6");
  44.             Console.WriteLine("Item Count after Add(): " + byRef.Items.Count);  //Items.Count == 6, still
  45.  
  46.             try
  47.             {
  48.                 Console.WriteLine("Getting item at Items[6]...");
  49.                 Console.WriteLine(byRef.Items[6]);  //throw exception
  50.             }
  51.             catch (Exception e)
  52.             {
  53.                 Console.WriteLine(e.Message);
  54.             }
  55.         }
  56.     }
  57. }
Sep 29 '09 #2
cloud255
427 Expert 256MB
Hmmm, this is very interesting. It appears that the issue arises due to the protection level of the items object. Have a look at the following:

Expand|Select|Wrap|Line Numbers
  1. interface IListInterface
  2.      {
  3.          ArrayList Items { get; set; }
  4.          void AddItem(object valueToAdd); //method to access the private item variable
  5.      }
  6.  
  7.      class ListClass : MarshalByRefObject, IListInterface
  8.      {
  9.  
  10.          private ArrayList items = new ArrayList{0,1,2,3,4,5};
  11.          public ArrayList Items 
  12.          { 
  13.              get
  14.              {
  15.                  return items;
  16.              } 
  17.              set
  18.              {
  19.                  items = value;
  20.              } 
  21.          }
  22.  
  23.         //method calls add function of the private variable
  24.          public void AddItem(object valueToAdd)
  25.          {
  26.              if(valueToAdd != null)
  27.                  items.Add(valueToAdd);
  28.          }
  29.      }
  30.  
  31.      class Program
  32.      {
  33.          static void Main(string[] args)
  34.          {
  35.              AppDomain ad = AppDomain.CreateDomain("OtherAppDomain");
  36.              IListInterface byRef = (IListInterface)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "b2.ListClass");
  37.  
  38.              foreach (object obj in byRef.Items)
  39.              {
  40.                  Console.WriteLine(obj);
  41.              }
  42.  
  43.              Console.WriteLine("Item Count before Add(): " + byRef.Items.Count);  //Items.Count == 6
  44.  
  45.              //add the value
  46.              byRef.AddItem("Item at index 6");
  47.              Console.WriteLine("Item Count after Add(): " + byRef.Items.Count);  //Items.Count == 7 now
  48.  
  49.              try
  50.              {
  51.                  Console.WriteLine("Getting item at Items[6]...");
  52.                  Console.WriteLine(byRef.Items[6]);  //exception is gone
  53.              }
  54.              catch (Exception e)
  55.              {
  56.                  Console.WriteLine(e.Message);
  57.              }
  58.              Console.ReadKey();
  59.          }
  60.      }
Only the public Items member can be accessed so you add an object to the Items list but when you call the get method of Items, the private items variable is returned. The private items variable has never had an object added to it, thus it still only contains the original 6 members. I may be mistaken here, but that is my reasoning of the situation...
Sep 29 '09 #3
Plater
7,872 Expert 4TB
I've never seen the SET section required for a collection object. Do to the nature of it, I believe just the GET is all that is required.
Maybe it will help?
I've never had a problem doing it (without an interface), but have never tried it with an interface
Sep 29 '09 #4
smulz
3
Thanks for your reply cloud. But can you explain that the same behavior happens when I'm directly modifying the public variable?

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7.  
  8. namespace WebQuestion
  9. {
  10.     interface IListInterface
  11.     {
  12.         ArrayList Items { get; set; }
  13.     }
  14.  
  15.     class ListClass : MarshalByRefObject, IListInterface
  16.     {
  17.         public ArrayList publicItems = new ArrayList { 0, 1, 2, 3, 4, 5 };
  18.         private ArrayList items = new ArrayList { 0, 1, 2, 3, 4, 5 };
  19.         public ArrayList Items 
  20.         { 
  21.             get
  22.             {
  23.                 return items;
  24.             } 
  25.             set
  26.             {
  27.                 items = value;
  28.             } 
  29.         }
  30.     }
  31.  
  32.     class Program
  33.     {
  34.         static void Main(string[] args)
  35.         {
  36.             AppDomain ad = AppDomain.CreateDomain("OtherAppDomain");
  37.             IListInterface byRef = (IListInterface)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "WebQuestion.ListClass");
  38.             foreach (object obj in byRef.Items)
  39.             {
  40.                 Console.WriteLine(obj);
  41.             }
  42.  
  43.             Console.WriteLine("Item Count before Add(): " + byRef.Items.Count);  //Items.Count == 6
  44.             byRef.Items.Add("Item at index 6");
  45.             Console.WriteLine("Item Count after Add(): " + byRef.Items.Count);   //Items.Count == 6, still
  46.  
  47.             try
  48.             {
  49.                 Console.WriteLine("Getting item at Items[6]...");
  50.                 Console.WriteLine(byRef.Items[6]);  //throw exception
  51.             }
  52.             catch (Exception e)
  53.             {
  54.                 Console.WriteLine(e.Message);
  55.             }
  56.  
  57.             ListClass listClass = (ListClass) byRef;
  58.  
  59.             foreach (object obj in listClass.publicItems)
  60.             {
  61.                 Console.WriteLine(obj);
  62.             }
  63.  
  64.             Console.WriteLine("Item Count before Add(): " + listClass.publicItems.Count);  //publicItems.Count == 6
  65.             listClass.publicItems.Add("Item at index 6");
  66.             Console.WriteLine("Item Count after Add(): " + listClass.publicItems.Count);   //publicItems.Count == 6, still
  67.  
  68.             try
  69.             {
  70.                 Console.WriteLine("Getting item at publicItems[6]...");
  71.                 Console.WriteLine(listClass.publicItems[6]);  //throw exception
  72.             }
  73.             catch (Exception e)
  74.             {
  75.                 Console.WriteLine(e.Message);
  76.             }
  77.         }
  78.     }
  79. }
Sep 30 '09 #5
Plater
7,872 Expert 4TB
From your original example (post#2), if you took the interface inheritance away from your class, did it work as it should? (Adding the item correctly)

Also, does this code work for you:
private ArrayList items = new ArrayList{0,1,2,3,4,5};
I get compile errors about the format.


EDIT: Using your sample code from the 2nd post, I changed the init line to this:
IListInterface byRef = (IListInterface)new ListClass();
And it worked correctly.
Sep 30 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Brandon Potter | last post by:
Trying to find the best way to find common entries in an x number of ArrayLists or arrays of integers. Curious if there is a method already available in .NET to do just this very thing. ...
0
by: James | last post by:
I see that variations on this question have appeared before but I'm still completely stumped. I'm developing an application with a fairly robust graphics component for 3D rendering. I've written...
1
by: godsella | last post by:
First i have two stored procedures, i have passed the values of each one into two different arraylists of dates. how can i compare the two arraylists of dates? Thanks in advance
5
by: drdave | last post by:
I would like to have ten arraylists created within a loop.. is there a conversion or something I can do to acheive this.. pseudo: Dim counter As Integer = 0 Dim ArrName As ArrayList ...
0
by: steve | last post by:
I'm looking for a code example how to compare the values in a given record in different arraylists two arraylists, two fields in each record, both defined as string I'm thinking that it's...
3
by: steve | last post by:
I need to compare the value of a field in a row on an arraylist with the value of a field on a second arraylist I have this bit of code working for arrays but cant get it working for arraylists The...
4
by: Andy in S. Jersey | last post by:
I would like to create an unknown number of Arraylists, meaning, I don't know how many I should create until runtime. I will be reading a table, and 0,1,2, or more fields have to be put into...
2
by: Andy in S. Jersey | last post by:
I would like to create an unknown number of ArrayLists, that is I don't know that until runtime. Of course I can do this if I knew I needed two: ArrayList IntervalArray1 = new ArrayList();...
1
by: Newbie19 | last post by:
I'm just learning java arrays/arraylists and was wondering what are the best books for learning java arrays/arraylists? I know practice is the best way to learn, but I have a hard time...
3
by: =?Utf-8?B?Sm9zaFA=?= | last post by:
Hi All, I am attempting to compare values in two arraylists to make sure all the values are the same. I am running into trouble with my code if both arraylists compare okay up until a point and I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.