简体   繁体   English

解决C#中方法重载的优先规则是什么?

[英]What are the rules of precedence in resolving the method overloading in C#?

I'm writing a serializer in which I want to make use of method overloads extensively, to serialize objects of types deriving from IEnumerable<T> , IDictionary<K,V> and so on. 我正在编写一个序列化器,在其中要广泛使用方法重载,以序列化从IEnumerable<T>IDictionary<K,V>等派生的类型的对象。

I also intend to use dynamic keyword to let CLR choose the correct overload based on the runtime type of the object to be serialized. 我还打算使用dynamic关键字让C​​LR根据要序列化的对象的运行时类型选择正确的重载。

Have a look at this code snippet: 看一下以下代码片段:

void Serialize<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
{
  Console.WriteLine("IDictionary<TKey, TValue>");
}

void Serialize<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> items)
{
  Console.WriteLine("IEnumerable<KeyValuePair<TKey, TValue>>");
}

void Serialize<T>(IEnumerable<T> items)
{
  Console.WriteLine("IEnumerable<T>");
}

And I want to do this: 我想这样做:

void CallSerialize(object obj)
{
   Serialize(obj as dynamic); //let the CLR resolve it at runtime.
}

Now based on the runtime-type of obj , the correct overload will be called. 现在基于obj的运行时类型,将调用正确的重载。 For example, 例如,

//Test code
CallSerialize(new List<int>()); //prints IEnumerable<T>

In this case, the third overload is called and the rationale is pretty much straightforward : that is only the viable option. 在这种情况下,将调用第三个重载,其原理非常简单:这只是可行的选择。

However, if I do this: 但是,如果我这样做:

CallSerialize(new Dictionary<int,int>()); //prints IDictionary<TKey, TValue>

It calls the first overload. 它称为第一个重载。 I don't exactly understand this. 我不太了解。 Why does it resolve to the first overload when all three overloads are viable options ? 当所有三个重载都是可行的选择时,为什么它会解决第一个重载?

In fact, if I remove the first one, the second overload is called, and if I remove the first and second overload, then the third overload is called. 实际上,如果删除第一个过载,则调用第二个重载,如果删除第一和第二个过载,则将调用第三个重载。

What are the rules of precedence in resolving the method overloading? 解决方法重载的优先规则是什么?

The rules for resolving method overloads will try to pick the method header with the most specific type match. 解决方法重载的规则将尝试选择类型最匹配的方法标头。 Here you can read more about overload resolution and here I think is your case. 在这里 ,你可以阅读更多有关重载决议和这里我想是你的情况。

From MSDN: 从MSDN:

Given an argument list A with a set of argument types {A1, A2, ..., AN} and two applicable function members MP and MQ with parameter types {P1, P2, ..., PN} and {Q1, Q2, ..., QN}, MP is defined to be a better function member than MQ if 给定具有一组参数类型{A1,A2,...,AN}的参数列表A和两个适用的函数成员MP和MQ,参数类型为{P1,P2,...,PN}和{Q1,Q2, ...,QN},如果满足以下条件,则MP被定义为比MQ更好的函数成员

  • for each argument, the implicit conversion from AX to PX is not worse than the implicit conversion from AX to QX, and 对于每个参数,从AX到PX的隐式转换不比从AX到QX的隐式转换差,并且

  • for at least one argument, the conversion from AX to PX is better than the conversion from >AX to QX. 对于至少一个参数,从AX到PX的转换要好于从> AX到QX的转换。

When performing this evaluation, if MP or MQ is applicable in its expanded form, then PX or QX refers to a parameter in the expanded form of the parameter list. 在执行此评估时,如果MP或MQ以其扩展形式适用,则PX或QX以参数列表的扩展形式引用一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM