M
Mark
Hi...
I'm trying to create a generic collection where the higher level code can
deal with more specific derivations, but they want to share some general
cleanup code. I've tried the following
public class MyBaseCollection<T> : Dictionary<string, T> where T :
MyBaseObject
{
}
then I have two branches that make MyBaseCollection<Obj1> and
MyBaseCollection<Obj2> where both Obj1 and Obj2 derive from MyBaseObject.
They both want to know about what the derivations do, but they want to call
a general
void Cleanup(MyBaseCollection<MyBaseObject> coll)
{
foreach (MyBaseObject in coll)
{ // do something
}
}
Now, I know I should probably add the Cleanup() function as a method of my
collection, but I was curious why MyBaseCollection<Obj1> wouldn't be castable
to MyBaseCollection<MyBaseObject> since the definition guarantees that T will
derive from MyBaseObject?
Thanks
Mark
I'm trying to create a generic collection where the higher level code can
deal with more specific derivations, but they want to share some general
cleanup code. I've tried the following
public class MyBaseCollection<T> : Dictionary<string, T> where T :
MyBaseObject
{
}
then I have two branches that make MyBaseCollection<Obj1> and
MyBaseCollection<Obj2> where both Obj1 and Obj2 derive from MyBaseObject.
They both want to know about what the derivations do, but they want to call
a general
void Cleanup(MyBaseCollection<MyBaseObject> coll)
{
foreach (MyBaseObject in coll)
{ // do something
}
}
Now, I know I should probably add the Cleanup() function as a method of my
collection, but I was curious why MyBaseCollection<Obj1> wouldn't be castable
to MyBaseCollection<MyBaseObject> since the definition guarantees that T will
derive from MyBaseObject?
Thanks
Mark