如何覆盖现有的扩展方法 [英] How to override an existing extension method

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

问题描述

我要替换包括在我自己的方法.NET或ASP MVC框架扩展方法。

I want to replace extension methods included in the .NET or ASP MVC framework by my own methods.

示例

public static string TextBox(this HtmlHelper htmlHelper, string name)
{
   ...
}

这可能吗?我不能使用override或者new关键字。

Is it possible? I can't use the override or new keyword.

推荐答案

更新:这个问题是的我在2013年的月博客的主题。感谢伟大的问题!

UPDATE: This question was the subject of my blog in December of 2013. Thanks for the great question!

您可以做到这一点,在一定意义上。但我要简要地谈到在C#中重载决议的基本设计原理开始。所有重载,当然,关于采取一系列方法具有相同的名称并选择从设置为来电独特的最好的成员。

You can do this, in a sense. But I should start by talking briefly about the basic design principle of overload resolution in C#. All overload resolution is, of course, about taking a set of methods with the same name and choosing from that set the unique best member to call.

有参与确定它是最佳的方法很多因素;不同的语言使用的因素,不同的混合物摸不着头脑。 C#在给定的方法来调用网站的大力尤其砝码亲近。如果给出一个基类或适用的方法在派生类中一种新的适用方法之间的选择,C#需要一个在派生类中,因为它更接近,即使一个在基类是在所有其他的方式更好匹配。

There are many factors involved in determining which is the "best" method; different languages use a different "mixture" of factors to figure this out. C# in particular heavily weights "closeness" of a given method to the call site. If given the choice between an applicable method in a base class or a new applicable method in a derived class, C# takes the one in the derived class because it is closer, even if the one in the base class is in every other way a better match.

因此​​,我们跑下列表中。派生类比基类更接近。内部类是比外部类近。在类层次结构方法比扩展方法接近。

And so we run down the list. Derived classes are closer than base classes. Inner classes are closer than outer classes. Methods in the class hierarchy are closer than extension methods.

现在我们来到你的问题。扩展方法的亲密程度取决于(1)有多少的命名空间走出去也我们必须去? (2)没有,我们发现通过使用扩展方法或者是它在命名空间在那里?因此,你可以通过在出现什么名称空间静态扩展类变化的影响重载解析,把它放在一个更接近命名空间中调用站点。或者,你可以改变你的使用的声明,把使用包含所需静态类比更接近的命名空间其他。

And now we come to your question. The closeness of an extension method depends on (1) how many namespaces "out" did we have to go? and (2) did we find the extension method via using or was it right there in the namespace? Therefore you can influence overload resolution by changing in what namespace your static extension class appears, to put it in a closer namespace to the call site. Or, you can change your using declarations, to put the using of the namespace that contains the desired static class closer than the other.

例如,如果您有

namespace FrobCo.Blorble
{
  using BazCo.TheirExtensionNamespace;
  using FrobCo.MyExtensionNamespace;
  ... some extension method call
}

那么它是不明确的更靠近。如果你想对他们到你的优先次序,你可以选择这样做:

then it is ambiguous which is closer. If you want to prioritize yours over theirs, you could choose to do this:

namespace FrobCo
{
  using BazCo.TheirExtensionNamespace;
  namespace Blorble
  {
    using FrobCo.MyExtensionNamespace;
    ... some extension method call
  }

现在当重载决议去解决扩展方法调用,在 Blorple 班会首先去的话,类 FrobCo.MyExtensionNamespace ,然后在 FrobCo ,然后类 BazCo.TheirExtensionNamespace

And now when overload resolution goes to resolve the extension method call, classes in Blorple get first go, then classes in FrobCo.MyExtensionNamespace, then classes in FrobCo, and then classes in BazCo.TheirExtensionNamespace.

清楚了吗?

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

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