Casting Nightmare
-
Ok I have found that this is not possible but I wanted to know if anyone knew a way around this problem. I have an abstract item class called AbstractMyItem, from this class I implement several items, one for example is MyBox. So the code for the box is:
public abstract class AbstractMyItem{}
public class MyBox: AbstractMyItem{}
For each item I want there to be a provider that performs some basic functionality for example:
public interface IMyItemProvider{
public AbstractMyItem [] LoadItems();
}So for each I create a provider from the interface:
public class MyBoxProvider : IMyItemProvider {
public AbstractMyItem [] LoadItems();
}However this creates a lot of casting as I have to cast the AbstractMyItem class to the MyBox class. It makes more sense to use generics like this:
public interface IMyItemProvider<T> where T : AbstractMyItem{
public T[] LoadItems();
}public class MyBoxProvider : IMyItemProvider<MyBox>{
public MyBox[] LoadItems();
}However as I said before I need to create a list of these providers. So in theory the obvious solution is:
List<IMyItemProvider<AbstractMyItem>> providers = new List<IMyItemProvider<AbstractMyItem>>();
providers.Add((IMyItemProvider<AbstractMyItem> ) MyBoxProvider);
However this will not work. You can't cast the concrete class to a the interfaced generic type. This does not work:
IMyItemProvider<AbstractMyItem> instance = (IMyItemProvider<AbstractMyItem> ) MyBoxProvider;
So the question. Anyone know of a good way to get around this problem? To be able to use generics with a list and assign my concrete classes to it? It would be nice to say (I know this not real code).
List<IMyItemProvider<inherits AbstractMyItem>> providers = new List<IMyItemProvider<inherits AbstractMyItem>>();
providers.Add((IMyItemProvider<AbstractMyItem> ) MyBoxProvider);
-
Ok I have found that this is not possible but I wanted to know if anyone knew a way around this problem. I have an abstract item class called AbstractMyItem, from this class I implement several items, one for example is MyBox. So the code for the box is:
public abstract class AbstractMyItem{}
public class MyBox: AbstractMyItem{}
For each item I want there to be a provider that performs some basic functionality for example:
public interface IMyItemProvider{
public AbstractMyItem [] LoadItems();
}So for each I create a provider from the interface:
public class MyBoxProvider : IMyItemProvider {
public AbstractMyItem [] LoadItems();
}However this creates a lot of casting as I have to cast the AbstractMyItem class to the MyBox class. It makes more sense to use generics like this:
public interface IMyItemProvider<T> where T : AbstractMyItem{
public T[] LoadItems();
}public class MyBoxProvider : IMyItemProvider<MyBox>{
public MyBox[] LoadItems();
}However as I said before I need to create a list of these providers. So in theory the obvious solution is:
List<IMyItemProvider<AbstractMyItem>> providers = new List<IMyItemProvider<AbstractMyItem>>();
providers.Add((IMyItemProvider<AbstractMyItem> ) MyBoxProvider);
However this will not work. You can't cast the concrete class to a the interfaced generic type. This does not work:
IMyItemProvider<AbstractMyItem> instance = (IMyItemProvider<AbstractMyItem> ) MyBoxProvider;
So the question. Anyone know of a good way to get around this problem? To be able to use generics with a list and assign my concrete classes to it? It would be nice to say (I know this not real code).
List<IMyItemProvider<inherits AbstractMyItem>> providers = new List<IMyItemProvider<inherits AbstractMyItem>>();
providers.Add((IMyItemProvider<AbstractMyItem> ) MyBoxProvider);
What you want to do is not possible as of C# 3. You may want to read Eric Lippert's series of articles on adding covariance and contravariance to a hypothetical version of C#[^]. In the meantime, here are some work arounds: -Use a non-generic list. -Make
IMyItemProvider
implement a non-generic interface, call itIProvidesItemsBase
, then make your list be of typeList<IProvidesItemsBase>
. This will work, of course when you call LoadItems, you'd get back an array ofAbstractMyItem
s.Life, family, faith: Give me a visit. From my latest post: "A lot of Christians struggle, perhaps at a subconscious level, about the phrase "God of Israel". After all, Israel's God is the God of Judaism, is He not? And the God of Christianity is not the God of Judaism, right?" Judah Himango