Создать instance по переменной типа "Тип" - C#
Формулировка задачи:
Привет. У меня есть такой код
Хочу сделать что-то вроде такого:
Не работает нифига....
var tuples = new List<Tuple<int, Type>>()
{
new Tuple<int, Type>(Rules.NumberOfAirCarriers, typeof(AirCarrier)),
new Tuple<int, Type>(Rules.NumberOfBattleships, typeof(Battleship)),
new Tuple<int, Type>(Rules.NumberOfCruisers, typeof(Cruiser)),
new Tuple<int, Type>(Rules.NumberOfDestroyers, typeof(Destroyer)),
new Tuple<int, Type>(Rules.NumberOfSubmarines, typeof(Submarine))
};foreach (var tuple in tuples)
{
for (int j = 0; j < tuple.Item1; j++)
{
Ship ship = Activator.CreateInstance(tuple.Item2);
}
}Решение задачи: «Создать instance по переменной типа "Тип"»
textual
Листинг программы
public class Destroyer : Ship
{
public Destroyer() : this (ShipType.Straight)
{
}
public Destroyer(ShipType type)
{
Type = type;
}
}