You can do it by casting the Card
instance appropriately:
public class Manager
{
public Card card1;
public Card card2;
}
...
var manager = new Manager
{
card1 = new CreatureCard();
card2 = new SpellCard();
}
if (manager.card1 is CreatureCard)
{
var health = ((CreatureCard)manager.card1).Health;
Console.WriteLine(health);
}
if (manager.card2 is SpellCard)
{
var effect = ((SpellCard)manager.card2).effect;
Console.WriteLine(effect);
}
Or like this:
var creatureCard = manager.card1 as CreatureCard;
var spellCard = manager.card2 as SpellCard;
if (creatureCard != null)
{
Console.WriteLine(creatureCard.health);
}
if (spellCard != null)
{
Console.WriteLine(spellCard.effect);
}
To expand on this:
To test if a card is of a specified type, use the
is
operator:bool isCardType = myCard is CardType;
Use
(CardType)
to cast / convert the card to the specified type:CardType specificCardType = (CardType)myCard;
To safely cast a card to a specific type or return null if it's not of that type, use the
as
operator:CardType specificCardType = myCard as CardType; if (specificCardType != null) { ... }