简体   繁体   中英

Can I add an extension method to derived classes that calls protected methods in the base?

We use a utility that generates code. I'd like to extend it and thought I might be able to do it without going back to the code generator. here is the problem.

The code generator gives me:

public abstract class BaseService
{
    protected virtual SqlCommand CreateSelectCommand(string whereClause, Hashtable parameters, string orderClause, int maxRows);
    protected DataSet Fill(SqlCommand command);
}

Then it generates lots of classes that implement this class. eg

public class ChildService : BaseService
{
    protected override SqlCommand CreateSelectCommand(string whereClause, Hashtable parameters, string orderClause, int maxRows)
    {
        //implementation here
    }

    protected override SqlCommand CreateSelectCommand(string whereClause, Hashtable parameters, string orderClause, int maxRows)
    {
        //implementation here
    }

    public virtual DataSet Select(string whereClause, System.Collections.Hashtable parameters, string orderClause, int maxRows)
    {
        SqlCommand command = CreateSelectCommand(whereClause, parameters, orderClause, maxRows);
        return Fill(command);
    }
}

Now I want to extend all the child classes to give me an override of the Select method. I tried an extension method something like this:

public DataSet Select(this BaseService service, string whereClause, Hashtable parameters, string orderBy, int startRowIndex, int maximumRows)
{
        SqlCommand command = service.CreateSelectCommand(whereClause, parameters, orderClause, maxRows);
        //Modify the command here
        return service.Fill(command);
}

But the compiler complains because the service parameter is not an instance of the child class and so cannot access the protected CreateSelectCommand and Fill methods. Is there any way around this so that I don't have to implement this in every child class?

Is there any way around this so that I don't have to implement this in every child class?

No, there isn't.

Extension methods are static methods on static classes that only appear (through the magic of intellisense) to "belong" to a type. This means that types need to match exactly (for one).

It also means that they do not belong to the type and don't have access to any protected/private members declared in the type.

No. An extension method is purely external to the class on which it operates. The only places you get access to a protected method is in the class itself (or a nested inner class), or in another class that inherits from it.

Incidentally, you are not providing an override at all by using an extension method...

No. An extension method is syntactic sugar for a static method invocation. In terms of accessibility the rules of an extension method are exactly that of a static one. Static methods can't violate items like protected and hence neither can extension methods

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