Somewhere in stackoverflow I found a question similar to my situation, which was like finding a needle in a haystack. But I foolishly closed my browser before I was completely done with my coding.
Here's my goal....
class BasicComponent
{
List<GroupIcon> icon;
}
class ItemGroup : BasicComponent
{
icon.Add(new ItemIcon(seed));
}
class MonsterGroup : BasicComponent
{
icon.Add(new MonsterIcon(seed));
}
class GroupIcon {}
class ItemIcon : GroupIcon {}
class MonsterIcon : GroupIcon {}
Using an interface for the icons worked great, until I needed additional methods for certain icons--not just different execution of the methods, but whole new methods. Since of course if the method wasn't defined in the interface the compiler couldn't find it, and I didn't want to add it to the interface and have to define it in all the other classes which didn't need it.
The answer I had found at stackoverflow was basically making my own List<> which only accepted certain types of objects. The code looked something like this:
class IconList<T> : List<Type>
{
public void Add(Type t)
{
if (!typeof(GroupIcon).IsAssignableFrom(t))
{
throw new ArgumentException("Type must be IGroupIcon, or implement/inherit it.");
}
else
{
base.Add(t);
}
}
}
But this isn't really working for me yet, so I have something wrong. For instance, this accessor...
public GroupIcon SelectedIcon
{
get { return icons[selectedPointer]; }
}
can't compile cause it's trying to convert Type into GroupIcon. And if I change it to Add(GroupIcon t) then IsAssignableFrom() isn't valid. (of course).
What'd I do wrong here?