为什么编译器发现这有歧义? [英] Why does the compiler find this ambiguous?

查看:139
本文介绍了为什么编译器发现这有歧义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的基类中,我有一个通用的方法(理想情况下,这将是一个属性,但是你不能有通用属性)和一个非通用属性,两者都具有相同的名称:

  protected static T CurrentUserId< T>()
{
...
}

protected static string CurrentUserId
{
get
{
...
}
}

但是,当我使用它们之间的intellisense报告歧义。当然, base.CurrentUserId (没有parethesese)提供足够的线索给编译器,我想调用非通用属性?



有人知道为什么编译器努力这样吗?提前感谢。

解决方案

保罗关于如何将泛型引入混合物似乎完全掩盖了这个更基本的问题的评论似乎是真的。



我相信你的问题与泛型无关,更多的关于如何适当地重载一个方法(我可能是错误的,但这是什么我从查找方法重载收集)。我甚至不确定一个属性是否可以通过一个具有相同名称的方法重载,但无论如何,让我们快速看看方法重载,这仍然应该证明你的代码不工作为什么。



如果方法的签名不同,您可以重载相同名称的方法(在您的情况下,名称为 CurrentUserId ) 。签名包括以下四个信息:




  • 方法名称

  • 参数个数

  • 参数的数据类型和顺序

  • 参数修饰符



根据Daniel Solis的说明C#2008 ,


类型是不是签名的一部分 - 虽然它是一个常见的错误,相信它是。


让我们看看你的属性和方法,看看他们是否可能有相同的签名根据上面提供的四条信息: / p>


  • 方法的名称是否相同?是, CurrentUserId

  • 参数数量是否相同?是,0。

  • 参数的数据类型和顺序是否相同?没有参数,因此是。

  • 参数修饰符是否相同?



当您删除参数时,从方程中,我们唯一唯一标识类型的是它的名字。另外,由于方法(或属性)的返回类型不是其签名的一部分,因此一个类型是字符串而另一个类型是通用类型并不重要。无论哪种方式,两者都有相同的签名(因为它们的名称相同,并且没有参数),编译器将无法适当地区分它们。



我不知道这是否准确地回答了你的问题(或者即使我正确地接近它...我很高兴欢迎在评论中的更正,以进一步我的个人理解)。但是,我发现通过首先正确理解重载来解决这个问题对于解决编译器处理代码的整体问题非常有帮助。



我希望这有助于。


In my base class I have a generic method (ideally this would be a property, but you can't have generic properties) and a non-generic property, both with the same name:

protected static T CurrentUserId<T>()
{
    ...
}

protected static string CurrentUserId
{
    get
    {
        ...
    }
}

However, when I come to use either of them intellisense reports ambiguity between them. Surely, base.CurrentUserId (without parethesese) provides enough clues to the compiler that I want to invoke the non-generic property?

Does anyone know why the compiler struggles with this? Thanks in advance.

解决方案

Paul's comment regarding how "introducing generics into the mix seems to entirely mask this more fundamental issue" seems true.

I believe that your problem has less to do with generics and more to do with how to appropriately overload a method (I could be wrong on this, but this is what I gather from looking up method overloading). I'm not even sure if a property can be overloaded by a method with the same name, but either way, let's take a quick look at method overloading, which still should demonstrate why your code doesn't work.

You can overload a method of the same name (in your case, the name is CurrentUserId) if the signatures of the methods are different. A signature consists in the following four pieces of information:

  • The name of the method
  • The number of parameters
  • The data types and order of the parameters
  • The parameter modifiers

According to Illustrated C# 2008 by Daniel Solis,

"The return type is not part of the signature--although it is a common mistake to believe that it is."

Let's look at your property and method and see if they might have the same signature according to the four pieces of information provided above:

  • Are the names of the method the same? Yes, CurrentUserId.
  • Are the number of parameters the same? Yes, 0.
  • Are the data types and order of the parameters the same? There are no parameters for either, so yes.
  • Are the parameter modifiers the same? Again, there are no parameters for neither the property nor the method, so yes, they are the same.

When you remove the parameters from the equation, the only thing we have to uniquely identify the type is its name. Also, since the return type of the method (or property) is not part of its signature, it doesn't matter that one type is a string and the other type is a generic. Either way, both have the same signature (because their names are the same and they have no parameters) and the compiler will not be able to appropriately distinguish them.

All of that being said, I'm not sure if this exactly answers your question (or even if I approached it correctly...I gladly welcome corrections in the comments to further my personal understanding). However, I find that approaching the problem by first correctly understanding overloading is very helpful to solving your overall problem of how the compiler is treating your code.

I hope this helps.

这篇关于为什么编译器发现这有歧义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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