Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get actual type of an derived class from its parent interface

Let's say we have a code portion like this:

IProduct product = ProductCreator.CreateProduct(); //Factory method we have here SellThisProduct(product);  //...  private void SellThisProduct(IProduct product) {   //.. Do something here }  //...  internal class Soda : IProduct {}  internal class Book : IProduct {} 

How can I infer which product is actually passed into SellThisProduct() method in the method?

I think if I say GetType() or something it will probably return the IProduct type.

like image 448
Tarik Avatar asked Mar 26 '10 01:03

Tarik


People also ask

How can we get derived class from base class?

Type classType = typeof(YourClass); Type baseType = classType. BaseType; string baseClassName = baseType.Name; Note that, if you recursively search the base types, when you call BaseType on typeof(System. Object), you'll get null.

Can we create the objects of the derived class?

Answer: No. In general, When we create object of a class, its reference is stored in stack memory and object is stored in heap and address of object is assigned to class reference.

What is a derived class in C#?

A derived class, in the context of C#, is a class created, or derived from another existing class. The existing class from which the derived class gets created through inheritance is known as base or super class.


1 Answers

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. 
like image 181
John Feminella Avatar answered Oct 18 '22 12:10

John Feminella