解决方法重载时有没有优先权? [英] Is there any priority when resolving method overloads?

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

问题描述

为什么执行 TestMethod< T>(params T [] input)方法而不是执行 TestMethod(object input)。我很疑惑为什么编译器会执行泛型方法。.net框架中是否有优先级层次结构?

  class TestClass 
{
static void Main()
{
TestMethod(Hello World);


static void TestMethod(object input)
{
Console.WriteLine(object);


static void TestMethod< T>(params T [] input)
{
Console.WriteLine(params T []);
}
}


解决方案

对您的问题的评论包含C#重载解析规范的链接,其中包含您需要深入回答问题的所有信息。这不是最简单的阅读,但很难找到你的案例的解决方案的实际路径,所以这里是发生了什么,据我可以告诉:



参数数组:

首先,我们需要看看 params 关键字的作用:由于它是开发人员方便的捷径,因此它与以下内容相同:

  static void TestMethod< T>(params T [] input)

被转换为



< pre $














$ static void TestMethod< T>

  TestMethod(string2,string2); 

会被转换为:

  TestMethod(new string [] {string1,string2); 

解决方案:



你的电话是:



  TestMethod(string); 

第一个选项是将此调用转换为:

  TestMethod(new string [] {string}); 

这将允许调用通用版本,同时使用 string 作为 T ,这将导致'已解析方法签名':

  TestMethod(string [] item)

因此,需要从实际转换为必需的转换参数类型是

  string [] to string [] 

选项b是将参数string解释为我们眼中的内容,例如字符串。然后调用非通用版本是可行的。

 字符串到对象

转换根据C#规范的7.4.3.4节进行评估,并避免将字符串降级为对象,第一个选项被选中。

Why does execute TestMethod<T>(params T[] input) method instead of execute TestMethod(object input).I am confusing why complier execute generic method.Is there any priority hierarchy on .net framework ?

class TestClass
{
    static void Main()
    {
        TestMethod("Hello World");
    }

    static void TestMethod(object input)
    {
        Console.WriteLine("object");
    }

    static void TestMethod<T>(params T[] input)
    {
        Console.WriteLine("params T[]");
    }
}

解决方案

The comment on your question contains the link to the overload resolution specs of C# which contains all infos you need to answer the question in depth. It's not the most easy read, though and hard to find the actual path of resolution in your case, so here is what happens, as far as I can tell:

Parameter arrays:

First, we need to look at what the params keyword does: As it is a shortcut for the developers convenience, here is what it is identical to:

static void TestMethod<T>(params T[] input)

gets translated to

static void TestMethod<T>(T[] item)

and calls get translated accordingly:

TestMethod("string2", "string2");

gets translated to:

TestMethod(new string[] { "string1", "string2" );

Resolution:

With this in mind, let's see what the compiler does: The compiler evaluates its options to resolve your call at runtime.

Your call is:

TestMethod("string"); 

The first option is to translate this call to:

TestMethod(new string[] { "string" }); 

This would allow to call the generic version, while using string as T, which would result in the 'resolved method signature':

TestMethod(string[] item)

So, the required conversion from actual to required argument type is

string[] to string[]

Option b is to interpret the parameter "string" as what it is in our eyes, as a string. Then it would be feasible to call the non-generic version.

string to object

The conversions are evaluated according to section 7.4.3.4 of the C# spec and to avoid the downgrading of string to object, the first option is chosen.

这篇关于解决方法重载时有没有优先权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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