How to hide extension methods from derived classes?

江枫思渺然 提交于 2020-01-13 11:51:09

问题


I have a base abstract class to implement template method pattern.

public abstract class OptionalParameter
{
    //Template Method Pattern
    public string GenerateQueryString()
    {
        return this.GenerateQueryStringWithParameters();
    }
}

And I have an extension method for all OptionalParameter type.

public static class OptionalParameterExtensions
{
    public static string GenerateQueryStringWithParameters(this OptionalParameter optionalParameters)
    {

    }
}

I inherited a class from OptionalParameter named CalendarEventParameter.

public class CalendarEventParameters : OptionalParameter
{

}

When I want to create an instance of CalenderEventParameter I see both GenerateQueryString() method in abstract class and GenerateQueryStringWithParameters() extension method.

I don't wan to see it from derived classes. I tried to sign extension method as Private but this way I cannot access also from my abstract class.

Is it possible to able to call extension method only from base abstract class?


回答1:


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
    }
}



回答2:


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.



来源:https://stackoverflow.com/questions/31788804/how-to-hide-extension-methods-from-derived-classes

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