Sven Groot wrote:
 | Minh wrote:You mean something like this? (Untested, BTW) |
Doesn't work. The is operator will check the object's real instantiated type, not the type of the variable which the OP wants.
I just typed in the example, and "is" doesn't return true for just the instantiated type.
It returns true for the entire class hierarchy.
ie,
Dog dog = new Dog();
dog is Dog == true
dog is Canine == true
dog is Animal == true
...
So, I'll revise my code... this seems to be working...
class Animal { ... }
class Canine : Animal { ... }
class Dog : Canine { ... }
void Test()
{
Dog dog = new Dog();
ProcessAnimal(dog);
Canine canine = dog as Canine;
ProcessAnimal(canine);
Animal animal = dog as Animal;
ProcessAnimal(animal);
}
void ProcessAnimal(Animal animal)
{
// Do Animal-specific stuff
}
void ProcessAnimal(Canine canine)
{
// Do Canine-specific stuff
}
void ProcessAnimal(Dog dog)
{
// Do Dog-specific stuff
}