Calling an extention method of a base class from child class

Otman IGHOULASSEN

Why in a child class, i can not call the extention method defined for the derived class by calling the base class directly ( i get a compile error that the base class does not contain a definition for the extention method). But instead, i can call the extention method without any compile errors when i call it directly from the child intance. Below is the code for my question :

using System;
using System.Reflection;

    public class Program
    {
      public static void Main()
      {
         Child child = new Child();
         child.TestMethod();
      }
    }

   // Derived class
   public class Mother
   {

   }

   // Child class
   public class Child : Mother
   {
     public Child() : base()
     {
     }

     public void TestMethod()
     {
       this.ExtentionMethod(3);// Ok: no compile errors
       base.ExtentionMethod(3);// Ko: Compilation error (line 27, col 8): 'Mother' does not contain a definition for 'ExtentionMethod'
     }
}

public static class Extender
{
   public static void ExtentionMethod(this Mother mother, int i)
   {
     Console.WriteLine($"Mother extention method {i}");
   }

}
D Stanley

When you call an extension method, the compiler looks at the type of the reference on the left and finds the most appropriate method. So when you call this.ExtentionMethod, the type of this is used to find the best method.

So in your case, the compiler will look for an extension with a Child first parameter. Since there is not one, it will then find the one with a Mother first parameter (since a Child "is-a" Mother).

Using base does not do a cast - it is used to access members of the base class. Since extension methods are not "members", base does not do what you expect it to do.

An alternative might be to cast this to the base class instead:

((Mother)this).ExtentionMethod(3);

Although I would note that you don't have a different extension method for the derived class, so with what you posted there would be no difference between this.ExtensionMethod and ((Mother)this).ExtensionBethod. The same method (with the same input values) are going to be called.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling base class method in Python

From Java

Calling base class overridden function from base class method

From Dev

Calling virtual method of base template from derived variadic template class

From Dev

Calling a private base method from a derived class in C#

From Dev

Calling the base class method from Code Quotations

From Dev

Calling method of template base class

From Dev

calling child class method from base class C#

From Dev

Design pattern: child class calling base class

From Dev

Return a new instance of Child class from base class static method

From Dev

Calling base class method after child class __init__ from base class __init__?

From Dev

Java - parent class is calling a method from a child class?

From Dev

Instantiating child class from a static method in base class, using TypeScript

From Dev

Calling a method from a derived class object that returns a base class pointer

From Dev

What are the best way to enhance base class method from child class

From Dev

python - remain in base class when calling parent method from child

From Dev

Calling a Method from "base.base" class?

From Dev

Calling a method from a class

From Dev

Calling derived class method from base class destructor

From Dev

Java - Calling method from child of abstract class

From Dev

Calling method of the base class (not the immediate parent class)

From Dev

How to obtain the derived class type from base when calling a method

From Dev

Why does calling a method from base class calls the child method?

From Dev

Calling an overloaded parent's method from a child class in C++

From Dev

Calling a method from base class to class with same name as the method

From Dev

Calling parent trait method from child class in Groovy

From Dev

Calling a parent method from a child class with a variable of that child

From Dev

How to access base class method *args from child class object?

From Dev

Calling child method from obj of parent class

From Dev

Calling child's overridden method from parent class

Related Related

  1. 1

    Calling base class method in Python

  2. 2

    Calling base class overridden function from base class method

  3. 3

    Calling virtual method of base template from derived variadic template class

  4. 4

    Calling a private base method from a derived class in C#

  5. 5

    Calling the base class method from Code Quotations

  6. 6

    Calling method of template base class

  7. 7

    calling child class method from base class C#

  8. 8

    Design pattern: child class calling base class

  9. 9

    Return a new instance of Child class from base class static method

  10. 10

    Calling base class method after child class __init__ from base class __init__?

  11. 11

    Java - parent class is calling a method from a child class?

  12. 12

    Instantiating child class from a static method in base class, using TypeScript

  13. 13

    Calling a method from a derived class object that returns a base class pointer

  14. 14

    What are the best way to enhance base class method from child class

  15. 15

    python - remain in base class when calling parent method from child

  16. 16

    Calling a Method from "base.base" class?

  17. 17

    Calling a method from a class

  18. 18

    Calling derived class method from base class destructor

  19. 19

    Java - Calling method from child of abstract class

  20. 20

    Calling method of the base class (not the immediate parent class)

  21. 21

    How to obtain the derived class type from base when calling a method

  22. 22

    Why does calling a method from base class calls the child method?

  23. 23

    Calling an overloaded parent's method from a child class in C++

  24. 24

    Calling a method from base class to class with same name as the method

  25. 25

    Calling parent trait method from child class in Groovy

  26. 26

    Calling a parent method from a child class with a variable of that child

  27. 27

    How to access base class method *args from child class object?

  28. 28

    Calling child method from obj of parent class

  29. 29

    Calling child's overridden method from parent class

HotTag

Archive