How to hide extension methods from derived classes?

狂风中的少年 提交于 2019-12-05 14:22:26

First off, if you want an extension method only for a class, and you have control of that class. You can make that method member of the class. Second you want a method to exist on a class but on its derived classes? That's not how it is supposed to work.

Try making that method internal, so you have total control on where it gets called. If you need it to be public, you can have it belong to an interface, and use an implicit implementation, that way it will only be available if you cast the object.

You may also consider to hide the member from intellisense, or making it obsolete...

But in all honesty, that is not how OOP should work, go rethink your design.


It may be worth noting that if you have an extension method with the same name and signature than an actual method, the actual method takes preference. So you can have the extension method for the base class, and add an actual method in the derived classes...

If you are doing that, why don't yuu just have a method and make it virtual. So the derived class can replace the implementation.


Look at your pattern - see comments in code:

public abstract class OptionalParameter
{
    public string GenerateQueryString()
    {
        // You are calling the extension method here
        return this.GenerateQueryStringWithParameters();
    }
}

That means that the extension method IS the default implementation. Just make that method virtual:

public abstract class OptionalParameter
{
    public virtual string GenerateQueryString()
    {
        // Default implementation, whatever
    }
}

And then, you can replace the implementation:

public class CalendarEventParameters : OptionalParameter
{
    public override string GenerateQueryString()
    {
        // Custom implementation
    }
}

No. Extension methods generally can't be hidden from derived classes.

It is only possible when the other class is inside another assembly than all the callers of the extension method (you could mark the extension method internal then).

You could also put the extension method inside another namespace, which you include in every class file you want to call the extension method on. This isn't true hiding, but it might do the job.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!