G
Guest
I have a question about using Generics with Interfaces and some of there
inheritance issues / problems. If this is not possible what I describe below
I will have to go a different route and would like some suggestions.
I am unable to use abstract classes as my code must not effect any current
inheritance chains so I am using interfaces. So I have something like this:
interface IBase
{
Collection<IBaseItem> Items{get;set;}
}
interface IBaseItem
{
string Title { get; set; }
}
Now that is the base and then we will have components that will extend this:
interface IAnotherBase : IBase
{
string Information {get; set; }
}
interface IAnotherBaseItem : IBaseItem
{
string OtherStuff { get; set; }
}
The question comes down to implementation. When implementing the second set
of classes the Collection<> should be of IAnotherBaseItem elements. So I
need to be able to cast this stuff out. As well any class that implements
the IAnotherBase class should enforce that the items being added to the
collection are of the correct type of IAnotherBaseItem class.
In trying to do something like this it won't let me:
class Stuff : IAnotherBase
{
private Collection<IAnotherBaseItem> _items;
// this is the interface from IBase
public Collection<IBaseItem> Items
{
// This fails
get { return this._items; }
set { this._items = value; }
}
}
What I need to be able to do is something I guess like interface shadowing
however I need to figure out how to cast this information out. Do I need to
use a secondary internal Collection<> of the IBaseItem as well as the
IAnotherBaseItem or something else?
Anything that anybody can help me with would be great.
Thanks.
inheritance issues / problems. If this is not possible what I describe below
I will have to go a different route and would like some suggestions.
I am unable to use abstract classes as my code must not effect any current
inheritance chains so I am using interfaces. So I have something like this:
interface IBase
{
Collection<IBaseItem> Items{get;set;}
}
interface IBaseItem
{
string Title { get; set; }
}
Now that is the base and then we will have components that will extend this:
interface IAnotherBase : IBase
{
string Information {get; set; }
}
interface IAnotherBaseItem : IBaseItem
{
string OtherStuff { get; set; }
}
The question comes down to implementation. When implementing the second set
of classes the Collection<> should be of IAnotherBaseItem elements. So I
need to be able to cast this stuff out. As well any class that implements
the IAnotherBase class should enforce that the items being added to the
collection are of the correct type of IAnotherBaseItem class.
In trying to do something like this it won't let me:
class Stuff : IAnotherBase
{
private Collection<IAnotherBaseItem> _items;
// this is the interface from IBase
public Collection<IBaseItem> Items
{
// This fails
get { return this._items; }
set { this._items = value; }
}
}
What I need to be able to do is something I guess like interface shadowing
however I need to figure out how to cast this information out. Do I need to
use a secondary internal Collection<> of the IBaseItem as well as the
IAnotherBaseItem or something else?
Anything that anybody can help me with would be great.
Thanks.