Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From base class in C#, get derived type?

Let's say we've got these two classes:

public class Derived : Base {     public Derived(string s)         : base(s)     { } }  public class Base {     protected Base(string s)     {      } } 

How can I find out from within the constructor of Base that Derived is the invoker? This is what I came up with:

public class Derived : Base {     public Derived(string s)         : base(typeof(Derived), s)     { } }  public class Base {     protected Base(Type type, string s)     {      } } 

Is there another way that doesn't require passing typeof(Derived), for example, some way of using reflection from within Base's constructor?

like image 571
core Avatar asked Jun 09 '09 21:06

core


People also ask

What is the base class from?

A base class is a class, in an object-oriented programming language, from which other classes are derived. It facilitates the creation of other classes that can reuse the code implicitly inherited from the base class (except constructors and destructors).

What are base and derived classes?

The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.

What is the meaning of base class in C?

Explanation: A class which is parent of another class, or from which other classes can be derived, is known as a base class.

What Is syntax of deriving a new class from base class is?

A class may be derived from a base class by using the inheritance syntax: class base { ... }; class derived : base { ... };

What is base class and derived class in C?

This existing class is called the base class, and the new class is referred to as the derived class. A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces. The following is the syntax of base class in C# −

What is base class in Java?

The Base Class, also known as the Parent Class or the Super Class is a class, from which other classes are derived. In other term it is a base class for other derived classes. That means if a derived class which inherits the base class has all members of a base class as well as can also have some additional properties.

What is a virtual base class in C++?

Virtual base class in C++. Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a given class appearing in an inheritance hierarchy when using multiple inheritances. Need for Virtual Base Classes: Consider the situation where we have one class A .This class is A is inherited by two other classes B...

How do you call the base class constructor from the derived constructor?

By default the derived constructor only calls the default base constructor with no parameters; so in this example, the base class constructor is NOT called automatically when the derived constructor is called, but it can be achieved simply by adding the base class constructor syntax after a colon (: ).


2 Answers

using System; using System.Collections.Generic; using System.Text;  namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {             Base b = new Base();             Derived1 d1 = new Derived1();             Derived2 d2 = new Derived2();             Base d3 = new Derived1();             Base d4 = new Derived2();             Console.ReadKey(true);         }     }      class Base     {         public Base()         {             Console.WriteLine("Base Constructor. Calling type: {0}", this.GetType().Name);         }     }      class Derived1 : Base { }     class Derived2 : Base { } } 

This program outputs the following:

Base Constructor: Calling type: Base Base Constructor: Calling type: Derived1 Base Constructor: Calling type: Derived2 Base Constructor: Calling type: Derived1 Base Constructor: Calling type: Derived2 
like image 117
Juliet Avatar answered Sep 21 '22 14:09

Juliet


GetType() would give you what you're looking for.

like image 34
Joe Enos Avatar answered Sep 21 '22 14:09

Joe Enos