简体   繁体   中英

How to hide extension methods from derived classes?

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM