If two classes (Bar1 and Bar2) inherit from the same base class (Foo)
public abstract class Foo
{
public abstract void Spawn(Settings settings);
}
public class Bar1 : Foo
{
float a;
int b;
public override void Spawn(Bar1Settings settings)
{
if (settings == null)
{
settings = new Bar1Settings();
}
a = settings.a;
b = settings.b;
}
}
public class Bar2 : Foo
{
bool a;
int b;
public override void Spawn(Bar2Settings settings)
{
if (settings == null)
{
settings = new Bar2Settings();
}
a = settings.a;
b = settings.b;
}
}
and all settings specific to each class inherit from a base class Settings
public abstract class Settings { }
public class Bar1Settings : Settings
{
public float a = 1.0f;
public int b = 5;
}
public class Bar2Settings : Settings
{
public bool a = true;
public int b = 76;
}
How can I load the correct settings for each class when they are stored in a list of settings?
List<Settings> settingsList = new List<Settings>();
for (int i = 0; i < settingsList.Count; ++i)
{
// Spawn correct Bar class from settings
}
Is this going to be possible, or should I store each type of derived settings class in a separate settings list and then spawn from that?
The abstract Settings class is still required for overriding the abstract Foo.Spawn(Settings settings) correctly though, is it not? As it currently stands, it doesn't work though as the abstract will only accept the base Settings class not the derived classes?!