Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

c# - Elegant Dynamic Type Casting

I'd appreciate your advice on the following:

I'm using polymorphism. I have a base class and 30 sub classes that inherit this base class. I'm up casting instances of these sub classes to the base class type so that they can be handled in a more generic fashion.

My question is this. I need to access a public property that is specific to a particular sub class. Do I need to write a giant case statement where I check the type and down cast accordingly in order to access the property I need or is there a more elegant solution?

static void Main(string[] args)
    {

        animal slyvester = new cat();
        animal lassie = new dog();
        animal silver = new horse();

        //  Big ugly type checking code. If I have 30 types to check is there a better way?
        if (slyvester.GetType() == typeof(cat)) {
            Console.WriteLine(((cat)(animal)slyvester).PurrStrength);
        }
        else if(slyvester.GetType() == typeof(dog)) {

        }
        else if (slyvester.GetType() == typeof(horse))
        {

        }

        Console.ReadLine();
    }
}

public class animal { 

}

public class cat : animal {

    private string _purrStrength = "Teeth Shattering";

    public string PurrStrength {
        get { return _purrStrength; }
        set { _purrStrength = value; }
    }

}

public class dog : animal { 

}

public class horse : animal { 

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should consider an interface based approach. With interfaces, you define a set of operations (a contract by which implementers must conform) which your types must define. E.g, we could define a base interface, IAnimal

public interface IAnimal
{
  string GetSound();
}

From which we can define some animal types:

public class Cat : IAnimal
{
  public string GetSound()
  {
    return "Meow!";
  }
}

public class Dog : IAnimal
{
  public string GetSound()
  {
    return "Woof!";
  }
}

Now, when we want to declare our animal, we declare it of type IAnimal:

IAnimal cat = new Cat();
IAnimal dog = new Dog();

Console.WriteLine(cat.GetSound());
Console.WriteLine(dog.GetSound());

You could go one step further, and specialise your animals:

public class Cat : IAnimal
{
  public virtual string GetSound()
  {
    return "Meow!";
  }
}

public class BigCat : Cat
{
  public override string GetSound()
  {
    return "Roar!";
  }
}

In the latter example, I can make a default implementation of the cat's GetSound method, and then override it for my big cat.

Interface based programming hides away the need to horrible type conversions, because an interface guarantees a set of operations that will be provided.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...