I am trying to create a class that will edit Lists of objects. Its constructor accepts any list of object that implements the 'INameable' interface. It's method can then add one more member to the list.
For Example:
public static class Data
{
//All of these object types implement the INameable interface
List<Apple> Apples = new List<Apple>();
List<Orange> Oranges = new List<Orange>();
List<Pear> Pears = new List<Pear>();
}
public class ListEditor
{
private List<INameable> _list;
private Type _type;
public ListEditor(List<INameable> l, Type t)
{
_list = l;
_type = t
}
public void AddMember()
{
var newObject Activator.CreateInstance(_type);
_list.Add((iNameable)newObject );
}
}
Program
{
//these lines are not working because the casting is invalid...
private ListEditor _appleEditor = new ListEditor((List<INameable>)Data.Apples, TypeOf(Apple));
private ListEditor _orangeEditor = new ListEditor((List<INameable>)Data.Oranges, TypeOf(Orange));
private ListEditor _pearEditor = new ListEditor((List<INameable>)Data.Pears, TypeOf(Pear));
}
Problems:
What is the correct syntax to cast List as List WITHOUT creating a new list (which would break the reference to the original list)