As what might be a continuation of this question.
I stepped into the need of having a list of class List<Foo> where I would like to restrict the access to the elements of the list, but not the access to the list as a whole, so that I can get/set the whole list, but not get/set any of its elements.
The challenge is that the type of the elements of the list needs to be a public class with all its members (that are properties) public due to serialization of the list elements.
Is there any way to implement this?
Thanks in advance
Edit:
I attempted this:
public class SomeClass()
{
private List<string> someList;
public IList<string> SomeList {
get { return someList.AsReadOnly(); }
}
}
And checked if there could be any method that provides my need that would be kind of get { return someList.AsWholeListGettable&SettableOnly(); } or get { return someList.AsNoAccessibleElementsList(); }
I also tried the [] operator overload to try to make the index operator kind of private. (The same behaviour for an array would also be accepatable).
class SampleCollection<T>
{
// Declare an array to store the data elements.
private T[] arr = new T[100];
// Define the indexer to allow client code to use [] notation.
private T this[int i]
{
private get { return arr[i]; }
private set { arr[i] = value; }
}
}
as in here.
So that from outside the class members wont be accesible. But I get this error:
