其中超载被称为又如何? [英] Which overload is called and how?

查看:135
本文介绍了其中超载被称为又如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些扩展的方法来处理一样,你会怎么做jQuery中的CSS类。由于我使用ASP.NET框架有两个扩展点我需要处理。

I wrote some extensions methods to handle the css class like how you would do it in jQuery. Since I'm using ASP.NET the framework has two extension points i need to handle.

第一个是 System.Web.UI.WebControls.WebControl

public static bool HasCssClass(this WebControl control, string className)
public static void AddCssClass(this WebControl control, string className)
public static void RemoveCssClass(this WebControl control, string className)

和第二个是System.Web.UI.IAttributeAccessor ​​

and the second one is System.Web.UI.IAttributeAccessor

public static bool HasCssClass(this IAttributeAccessor accessor, string className)
public static void AddCssClass(this IAttributeAccessor accessor, string className)
public static void RemoveCssClass(this IAttributeAccessor accessor, string className)

两难这里,是因为有那的WebControl的CssClass属性似乎没有,当你通过IAttributeAccessor接口访问属性,以反映相应。

the dilemma here has to do with that the CssClass property of WebControl doesn't seem to reflect accordingly when you access the property through the IAttributeAccessor interface.

WebControl c;
c.CssClass = "hello-world";
Debug.Assert(((IAttributeSelector)c).GetAttribute("class") != "hello-world");

我做了一些与.net反射周围挖,来到该声明将举行的结论。我可以修改两种不同的方式相同的底层的HTML属性。因此,我的问题,这重载我用,或者更确切地说,这重载该编译器使用?

I did some digging around with the .NET Reflector and came to the conclusion that the assertion will hold. I can modify the same underlying HTML property class in two distinct ways. Thus my problem, which overload do I use, or rather, which overload does the compiler use?

现在, System.Web.UI.WebControls.WebControl 工具 System.Web.UI.IAttributeAccessor ​​这就是为什么我问的问题,它的过载将被使用?或者是呼叫暧昧两者之间?

Now, System.Web.UI.WebControls.WebControl implements System.Web.UI.IAttributeAccessor and this is why I ask the question, which overload will be used? Or is the call ambiguous between the two?

我总是想象编译/运行时间计算出的距离选择一个类型比其他。

I always imagine that the compiler/run-time calculated the distance to pick one type over the other.

文本框,这源于的WebControl 实现 IAttributeAccessor ​​将调用的WebControl 超载因为距离的WebControl 较短,严格地说。

Say TextBox, which derives from WebControl that implements IAttributeAccessor would call the WebControl overload because the distance to WebControl is shorter, strictly speaking.

我发现了一些东西在 MSDN 关于这一点,但是,它并没有说是什么意思比大于或好或坏。所以,这是怎么回事,当我打电话的情况发生。

I found some stuff on MSDN about this, however, it doesn't say what it means to be better than or worse than. So what's going to happen when I call.

TextBox c;
c.AddCssClass("hello-world");

我结束了改变IAttributeAccessor到HTMLControl时相反,监守它仍然涵盖了大多数情况下,并会减少混乱为其他开发人员在我的球队。

I ended up changing the IAttributeAccessor to HtmlControl instead, becuase it still covers most cases and would be less confusing for other developers on my team.

智能感知实际显示既尽可能过载,而只有一个,实际上,是可能的。

IntelliSense actually displayed both as possible overloads, while only one, in fact, is possible.

推荐答案

您几乎没有 - 你想部分的 7.4.2.3 代替 - 更好的转换。这不是转换距离的问题不过。 (在C#3规格这是如果你有兴趣7.4.3.4。)

You were nearly there - you wanted section 7.4.2.3 instead - "better conversion". It's not a matter of conversion "distance" though. (In the C# 3 spec this is 7.4.3.4 in case you're interested.)

文本框来转化的WebControl 比转换成 IAttributeSelector ,因为:

The conversion from TextBox to WebControl is better than the conversion to IAttributeSelector, because:

如果隐式转换,从T1到T2的存在,并没有从T2的隐式转换为T1   存在,C1是更好的转换。

If an implicit conversion from T1 to T2 exists, and no implicit conversion from T2 to T1 exists, C1 is the better conversion.

有从的WebControl 的隐式转换为 IAttributeSelector 而不是相反。

There's an implicit conversion from WebControl to IAttributeSelector but not vice versa.

现在,我们知道哪个转换比较好,我们可以将您参考,以确定与的WebControl 过载会有所回升的部分。这可以很容易地证明当然是有一个测试程序:

Now that we know which conversion is better, we can apply the section you referenced to determine that the overload with WebControl will be picked. This can easily be demonstrated with a test application of course:

using System;

public interface IFoo {}
public class Bar : IFoo {}
public class Baz : Bar {}

public static class Extensions
{
    public static void Extension(this IFoo foo)
    {
        Console.WriteLine("Extension(IFoo)");
    }

    public static void Extension(this Bar bar)
    {
        Console.WriteLine("Extension(Bar)");
    }
}

public class Test
{
    static void Main()
    {
        Baz b = new Baz();
        b.Extension();
    }
}

这版画扩展(酒吧) - 对应的扩展方法通过的WebControl 您的真实情况

This prints "Extension(Bar)" - corresponding to the extension method via WebControl in your real case.

当然,如果你投以 B 的IFoo 则超负荷打印扩展(的IFoo)将采摘 - 因为编译器不知道其他的过载将适用。请记住,重载的只有的在编译时进行(撇开在C#中的动态 4)。

Of course, if you cast to b to IFoo then the overload printing "Extension(IFoo)" will be picked - because the compiler doesn't know that the other overload will be applicable. Remember that overloading is only performed at compile time (leaving aside dynamic in C# 4).

这篇关于其中超载被称为又如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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