Until I tried to do this, I assumed that I knew how this worked; however, the following code throws an exception. Because TestOC
is a child of ObservableCollection
, I thought I could do this:
class MyClass
{
public string MyProperty { get; set; }
}
class TestOC : ObservableCollection<MyClass>
{
}
class Program
{
static void Main(string[] args)
{
ObservableCollection<MyClass> test = new ObservableCollection<MyClass>();
test.Add(new MyClass());
TestOC test2 = (TestOC)test;
}
}
The error thrown is:
Unable to cast object of type 'System.Collections.ObjectModel.ObservableCollection`1[ConsoleApplication17.MyClass]' to type 'ConsoleApplication17.TestOC'.
How can I get the ObservableCollection
to assign to my child class here?