How to hide extension methods from derived classes?

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

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

And I have an extension method for all OptionalParameter types.

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

I inherited a class from the OptionalParameter parameter named CalendarEventParameter.

 public class CalendarEventParameters : OptionalParameter { } 

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

I do not want to see this from derived classes. I tried to sign the extension method as private, but in this way I can not get access from my abstract class either.

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

+4
source share
2 answers

First of all, if you want to use the extension method only for the class, and you have control over this class. You can make this member a class method. Secondly, do you want the method to exist in the class, but on its derived classes? This is not how it should work.

Try to make this method internal, so you have full control over where it is called. If you need it to be publicly available, you can have it belong to the interface and use an implicit implementation, so it will only be available if you create an object.

You can also hide an element from intellisense or make it obsolete ...

But honestly, this does not mean that OOP should work, rethink your design.


It may be worth noting that if you have an extension method with the same name and signature as the actual method, then the preferred method takes preference. That way you can have an extension method for the base class and add the actual method to the derived classes ...

If you do this, why yuu just doesn't have a method and make it virtual. Thus, a derived class can replace an implementation.


Look at your template - see the comments in the code:

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

This means that the extension method is the default implementation. Just make this 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
source

Not. Extension methods usually cannot be hidden from derived classes.

This is only possible if another class is inside a different assembly than all the callers of the extension method (then you can mark the extension method internal ).

You can also put the extension method in a different namespace, which you include in each class file that you want to call the extension method on. This does not truly hide, but can do the job.

+5
source

All Articles