Hi consider the following code.
public interface IStuff<T>
{
IList<T> Items { get; set; }
IsExpanded { get; set; }
}
public interface IBar
{
string Name { get; set; }
}
public interface IFoo : IBar, IStuff<IBar>
{
}
public class MyClass : IFoo
{
public Name {get;set;
public IList<IBar> Items { get; set;}
IsExpanded { get; set; }
}
When I use this, what I'm actually going to is is populate MyClass.Items with instances of IFoo, this is allowed as IFoo implements IBar, so all is good.
However there is a method that I'm working inside a control, which does not know and should not know about IBar. It only needs to know about IStuff<> Specifically that there is a list and a property.
When I'm working with the Items, I'm going to detect if the item implements IStuff like this.
public void ProcessItems( object item )
{
IStuff<object> stuff = item as IStuffobject>;
stuff.IsExpanded = true;
foreach( var childItem in stuff.items )
{
ProcessItems( childItem );
}
}
The problem here is that the cast to IStuff does not now, it returns null. What I want to do is work with the genric part of the interface only, I don't care what the T is.
The question is how do I replace
IStuff<object> stuff = item as IStuffobject>;
so that I can work with the items declared in the IStuff<> interface?
Aucun commentaire:
Enregistrer un commentaire