GetType
gets you the exact runtime type of an object. From the documentation:
The Type instance that represents the exact runtime type of the current instance.
You can also use is
to determine if an object is an instance of a specific type:
var noise = (obj is Velociraptor) ? "SKREEE!" : "<unknown>";
Why do you need the exact runtime type, though? The entire point of an interface is that you should be hiding the implementation details behind the common interface. If you need to take an action based on the type, that's a big hint that you're violating the encapsulation it provides.
One alternative is to use polymorphism:
public interface IVocalizer { string Talk(); }
public class Doorbell : IVocalizer {
public string Talk() { return "Ding-dong!" }
}
public class Pokemon : IVocalizer {
public string Talk() {
var name = this.GetType().ToString();
return (name + ", " + name + "!").ToUpper(); } // e.g., "PIKACHU, PIKACHU!"
}
public class Human : IVocalizer {
public string Talk() { return "Hello!"; }
}
Since these three types aren't related at all, inheritance from a common type doesn't make sense. But to represent that they share the same capability of making noise, we can use the IVocalizer interface, and then ask each one to make a noise. This is a much cleaner approach: now you don't need to care what type the object is when you want to ask it to make a noise:
IVocalizer talker = new ???(); // Anything that's an IVocalizer can go here.
// elsewhere:
Console.WriteLine(talker.Talk()); // <-- Now it doesn't matter what the actual type is!
// This will work with any IVocalizer and you don't
// need to know the details.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…