Home > Backend Development > C++ > How to Prevent Intermediary Class Method Calls When Overriding in C#?

How to Prevent Intermediary Class Method Calls When Overriding in C#?

Patricia Arquette
Release: 2024-12-26 20:51:10
Original
862 people have browsed it

How to Prevent Intermediary Class Method Calls When Overriding in C#?

How to Avoid Intermediary Override when Calling Base Methods

When working with multiple levels of class hierarchy, it's possible to encounter situations where you want to call a base method from an overriding method, but you don't want to invoke the implementation of the intermediary class. This article will explore how to achieve this in C#.

In the provided code snippet, you have a Base class with a virtual method Say(). Derived class overrides this method, while SpecialDerived further overrides it. When calling sd.Say() for an instance of SpecialDerived, you notice that not only your own implementation is invoked but also Derived's.

To prevent Derived's implementation from being called, you can't use base.base.Say(). Instead, you have two options:

Option 1: Move Commonly Used Implementations to a Protected Virtual Method

If you have commonly used implementations that should be shared by multiple derived classes, move them to a protected virtual method in the base class. This allows other classes to override only the implementation they need, avoiding calling the intermediary class's implementation.

For example, instead of Derived overriding Say(), you can introduce a protected virtual method CustomSay():

class Base
{
    public virtual void Say()
    {
        Console.WriteLine("Called from Base.");
    }
}

class Derived : Base
{
    public override void Say()
    {
        CustomSay();

        base.Say();
    }

    protected virtual void CustomSay()
    {
        Console.WriteLine("Called from Derived.");
    }
}

class SpecialDerived : Derived
{
    protected override void CustomSay()
    {
        Console.WriteLine("Called from Special Derived.");
    }
}
Copy after login

Option 2: Direct Function Pointer Invocation (Unsafe Approach)

As an alternative, you can use the unsafe code block to directly invoke the function pointer for the base class's method. This approach is not recommended as it's considered unsafe and can lead to potential errors.

class SpecialDerived : Derived
{
    public override void Say()
    {
        Console.WriteLine("Called from Special Derived.");
        var ptr = typeof(Base).GetMethod("Say").MethodHandle.GetFunctionPointer();            
        var baseSay = (Action)Activator.CreateInstance(typeof(Action), this, ptr);
        baseSay();            
    }
}
Copy after login

The above is the detailed content of How to Prevent Intermediary Class Method Calls When Overriding in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template