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?
5 1666
Here's an example of what I mean... - 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);
-
}
-
}
-
}
-
}
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: - interface IListInterface
-
{
-
ArrayList Items { get; set; }
-
void AddItem(object valueToAdd); //method to access the private item variable
-
}
-
-
class ListClass : MarshalByRefObject, IListInterface
-
{
-
-
private ArrayList items = new ArrayList{0,1,2,3,4,5};
-
public ArrayList Items
-
{
-
get
-
{
-
return items;
-
}
-
set
-
{
-
items = value;
-
}
-
}
-
-
//method calls add function of the private variable
-
public void AddItem(object valueToAdd)
-
{
-
if(valueToAdd != null)
-
items.Add(valueToAdd);
-
}
-
}
-
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
AppDomain ad = AppDomain.CreateDomain("OtherAppDomain");
-
IListInterface byRef = (IListInterface)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "b2.ListClass");
-
-
foreach (object obj in byRef.Items)
-
{
-
Console.WriteLine(obj);
-
}
-
-
Console.WriteLine("Item Count before Add(): " + byRef.Items.Count); //Items.Count == 6
-
-
//add the value
-
byRef.AddItem("Item at index 6");
-
Console.WriteLine("Item Count after Add(): " + byRef.Items.Count); //Items.Count == 7 now
-
-
try
-
{
-
Console.WriteLine("Getting item at Items[6]...");
-
Console.WriteLine(byRef.Items[6]); //exception is gone
-
}
-
catch (Exception e)
-
{
-
Console.WriteLine(e.Message);
-
}
-
Console.ReadKey();
-
}
-
}
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...
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
Thanks for your reply cloud. But can you explain that the same behavior happens when I'm directly modifying the public variable? - 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
-
{
-
public ArrayList publicItems = new ArrayList { 0, 1, 2, 3, 4, 5 };
-
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);
-
}
-
-
ListClass listClass = (ListClass) byRef;
-
-
foreach (object obj in listClass.publicItems)
-
{
-
Console.WriteLine(obj);
-
}
-
-
Console.WriteLine("Item Count before Add(): " + listClass.publicItems.Count); //publicItems.Count == 6
-
listClass.publicItems.Add("Item at index 6");
-
Console.WriteLine("Item Count after Add(): " + listClass.publicItems.Count); //publicItems.Count == 6, still
-
-
try
-
{
-
Console.WriteLine("Getting item at publicItems[6]...");
-
Console.WriteLine(listClass.publicItems[6]); //throw exception
-
}
-
catch (Exception e)
-
{
-
Console.WriteLine(e.Message);
-
}
-
}
-
}
-
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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.
...
|
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...
|
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
|
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
...
|
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...
|
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...
|
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...
|
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();...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
| |