如何从派生类隐藏扩展方法? [英] How to hide extension methods from derived classes?

查看:150
本文介绍了如何从派生类隐藏扩展方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本抽象类来实现模板方法模式。

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

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

我有一个所有OptionalParameter类型的扩展方法。

And I have an extension method for all OptionalParameter type.

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

    }
}

我从OptionalParameter继承了一个类名为CalendarEventParameter。

I inherited a class from OptionalParameter named CalendarEventParameter.

public class CalendarEventParameters : OptionalParameter
{

}

当我想创建一个 CalenderEventParameter 的实例时,我看到两者抽象类中的GenerateQueryString()方法和 GenerateQueryStringWithParameters()扩展方法。

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

我不想从派生类中看到它。我试图将扩展方法签名为Private,但这样我也无法从我的抽象类访问。

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?

推荐答案

首先,如果你只想要一个类的扩展方法,你就可以控制那个类。您可以使该方法成为该类的成员。
第二,你希望一个方法存在于一个类上但是在它的派生类上?这不是它应该如何工作。

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.

你也可以考虑将会员隐藏在intellisense中,或者让它过时......

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

但老实说,这不是OOP应该如何运作,重新考虑你的设计。

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

如果你这样做,为什么不yuu只需要一个方法并使其成为虚拟的。因此派生类可以替换实现。

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

这篇关于如何从派生类隐藏扩展方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆