I have a collection class like this:
public class SomeDataCollection : List<ISomeData>
{
// some method ...
}
but I can't do this:
SomeDataCollection someDatas = new List<ISomeData>();
Cannot implicitly convert type
List<ISomeData>
toSomeDataCollection
. An explicit conversion exists (are you missing a cast?)
so I try to create an implicit coverter inside SomeDataCollection
collection class:
public static implicit operator SomeDataCollection(List<ISomeData> l)
{
var someDatas = new SomeDataCollection();
someDatas.AddRange(l);
return someDatas;
}
but it said that I can't create such converter:
SomeDataCollection.implicit operator SomeDataCollection(List<ISomeData>)
: user-defined conversions to or from a base class are not allowed
And when I cast it like this:
SomeDataCollection someDatas = (SomeDataCollection)new List<ISomeData>();
it throws an error that said:
System.InvalidCastException: Unable to cast object of type
List<ISomeData>
to typeSomeDataCollection
.
How can I do this:
SomeDataCollection someDatas = new List<ISomeData>();
without getting an error? Please help. Thanks in advance.