重载怪胎 [英] Overload resolution oddity

查看:124
本文介绍了重载怪胎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道这是C#4+具体,只是注意到这一点。

Not sure if this is C# 4+ specific, but just noticed this.

考虑如下的类:

class Base
{
  protected void Foo(object bar, DayOfWeek day)
  {
  }
}

class Program : Base
{
  protected void Foo(object bar, object baz)
  {
  }

  void Bar(DayOfWeek day)
  {
    Foo(new { day }, day);
  }
}

酒吧的调用,解析为美孚(对象,对象)

虽然将其更改为:

class Base
{

}

class Program : Base
{
  protected void Foo(object bar, object baz)
  {
  }

  protected void Foo(object bar, DayOfWeek day)
  {
  }

  void Bar(DayOfWeek day)
  {
    Foo(new { day }, day);
  }
}

酒吧的调用,解析为美孚(对象,星期几)

我的理解是,它应该始终解析为第二个例子。

My understanding is that it should always resolve as in the second example.

这是一个错误或只是我缺乏了解(或无知)?

Is this a 'bug' or just my lack of understanding (or ignorance)?

更新:

感谢您的答案。正如我已经找到了,你可以使用基地。来调用基类中的方法。在混合添加其他派生的类时,问题就来了但是回来

Thanks for the answers. As I have found out, one can use base. to call the method in the base class. The problem comes back however when adding another derived class in the mix.

class Base
{
  protected void Foo(object bar, DayOfWeek day)
  {
  }
}

class Program : Base
{
  protected void Foo(object bar, object baz)
  {
  }

  void Bar(DayOfWeek day)
  {
    base.Foo(new { day }, day);
  }
}

class Derived : Program
{
  void Baz(DayOfWeek day)
  {
    base.Foo(new { day }, day);
  }
}

基地。程序调用的作品,但随后解析为美孚(对象,对象)导出

The base. call works in Program, but then resolves to Foo(object, object) in Derived.

如何将一个呼叫美孚(对象,星期几)派生则无需创建'多余'的方法在计划

How would one call Foo(object,DayOfWeek) from Derived then without having to create 'redundant' methods in Program ?

推荐答案

我认为解决的方法调用,它看起来在同级车第一,因为星期几可以通过为对象键入,它调用类自己的方法,而不是从基类之一。

I think for resolving method call it looks in its class first, since DayOfWeek can be passed as object type, it calls class own method, not the one from the base class.

在第二种情况下,该方法调用解析为一个较为特殊类型的参数,因此美孚(对象吧,星期天)被调用。

In the second case, the method call resolves to a the more particular type parameter, therefore Foo(object bar, DayOfWeek day) gets called.

从MSDN - 方法解析

From MSDN - Method resolution.

基类的方法不是候选人,如果在派生的任何方法   类是适用(第 7.5.5.1 )。

methods in a base class are not candidates if any method in a derived class is applicable (Section 7.5.5.1).

      
  • 在给定了适用的候选函数成员,在该集合最好的函数成员。
  •   
  • 如果集合中只包含一个函数成员,则该函数成员为最佳函数成员。
  •   
  • 否则,最好的函数成员是一个函数成员比所有其他函数成员对于给定的   参数列表,只要每个函数成员相比,所有   使用规则第7.4.2.2其他函数成员。
  •   
  • 如果不是正好有一个函数成员比所有其他函数成员都好,则函数成员调用不   发生模糊,编译时错误。
  •   
  • Given the set of applicable candidate function members, the best function member in that set is located.
  • If the set contains only one function member, then that function member is the best function member.
  • Otherwise, the best function member is the one function member that is better than all other function members with respect to the given argument list, provided that each function member is compared to all other function members using the rules in Section 7.4.2.2.
  • If there is not exactly one function member that is better than all other function members, then the function member invocation is ambiguous and a compile-time error occurs.

这篇关于重载怪胎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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