编译器解析为错误的重载构造函数或方法 [英] Compiler resolves to wrong overloaded constructor or method

查看:88
本文介绍了编译器解析为错误的重载构造函数或方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是C#编译器或CLR运行时中的错误吗?

enum MyEnum {ZERO = 0,ONE = 1,TWO = 2}

class Foo {

public Foo(string,object){...}

public Foo(string,MyEnum){...}

}


Foo f = new Foo("",0); //使用Foo(string,MyEnum)构造函数代替

Foo(字符串,对象)

Foo f = new Foo("",0.0); //使用Foo(string,MyEnum)构造函数

of Foo(string,object)

Foo f = new Foo("",(int)0) ; //使用Foo(string,MyEnum)构造函数

而不是Foo(字符串,对象)

但是

Foo f = new Foo(" ;,1); //使用Foo(字符串,对象)

Foo f = new Foo("",(object)(int)0); //使用Foo(字符串,对象)

对象类型Int32

Foo f = new Foo("",(double)0.0); //使用Foo(字符串,对象)


实际案例来自ADO.NET数据提供程序的PrvParameter构造函数!

同样的问题出现了解决各种重载

PrvParameterCollection.Add!


还有人见过吗?

Philippe

Is this a bug in the C# compiler or CLR runtime?

enum MyEnum { ZERO = 0, ONE = 1, TWO = 2 }
class Foo {
public Foo(string,object) { ... }
public Foo(string,MyEnum) { ... }
}

Foo f = new Foo("", 0); // Uses Foo(string,MyEnum) constructor instead of
Foo(string,object)
Foo f = new Foo("", 0.0); // Uses Foo(string,MyEnum) constructor instead
of Foo(string,object)
Foo f = new Foo("", (int)0); // Uses Foo(string,MyEnum) constructor
instead of Foo(string,object)
BUT
Foo f = new Foo("", 1 ); // Uses Foo(string,object)
Foo f = new Foo("", (object)(int)0 ); // Uses Foo(string,object) with
object type Int32
Foo f = new Foo("", (double)0.0 ); // Uses Foo(string,object)

Actual case was from a ADO.NET Data Provider''s PrvParameter constructors!
Same issue came up resolving the various overloads of
PrvParameterCollection.Add!

Anybody else seen this?
Philippe

推荐答案

Philippe,


Haven没见过。


你怎么知道当你执行

新Foo(",",0)时,它正在使用Foo(string,MyEnum)构造函数?你单步执行代码时是否看到了这个?

如果没有,你应该单调调试器以确保你认为

正在执行的是真的执行。如果你单步并且*仍然*看到这个

行为,那么我会排除调试显示中的一个错误(它发生在

过去,在未修补的CLR 1.0)中将DebugWriteLine()语句放入

构造函数并确保它们确实作为调试器执行

显示。


--BOB


" Philippe Bertrand" <我的***** @ ianywhere.com>在消息中写道

新闻:10 ************* @ corp.supernews.com ...
Philippe,

Haven''t seen it.

How do you know it''s using the Foo(string,MyEnum) constructor when you do a
new Foo("",0)? Are you seeing this when you single-step through the code?
If not, you should single-step with the debugger to make sure what you think
is executing is really executing. If you single-step and *still* see this
behavior, then I would rule out a bug in the debug display (it''s happened in
the past, in unpatched CLR 1.0) by putting DebugWriteLine() statements into
the constructors and making sure they really DO execute as the debugger
shows.

--Bob

"Philippe Bertrand" <my*****@ianywhere.com> wrote in message
news:10*************@corp.supernews.com...
这是一个错误吗? C#编译器还是CLR运行时?

枚举MyEnum {ZERO = 0,ONE = 1,TWO = 2}
class Foo {
public Foo(string,object){..公共Foo(字符串,MyEnum){...}
}
Foo f = new Foo("",0); //使用Foo(string,MyEnum)构造函数代替Foo的
(字符串,对象)
Foo f = new Foo("",0.0); //使用Foo(string,MyEnum)构造函数
而不是Foo(字符串,对象)
Foo f = new Foo("",(int)0); //使用Foo(string,MyEnum)构造函数
代替Foo(字符串,对象)
但是Foo f = new Foo(",1); //使用Foo(字符串,对象)
Foo f = new Foo("",(object)(int)0); //使用具有对象类型的Foo(字符串,对象)Int32
Foo f = new Foo("",(double)0.0); //使用Foo(字符串,对象)

实际案例来自ADO.NET数据提供程序的PrvParameter构造函数!
同样的问题出现了解决各种重载的问题
PrvParameterCollection.Add!

其他人看过这个吗?
Philippe
Is this a bug in the C# compiler or CLR runtime?

enum MyEnum { ZERO = 0, ONE = 1, TWO = 2 }
class Foo {
public Foo(string,object) { ... }
public Foo(string,MyEnum) { ... }
}

Foo f = new Foo("", 0); // Uses Foo(string,MyEnum) constructor instead of Foo(string,object)
Foo f = new Foo("", 0.0); // Uses Foo(string,MyEnum) constructor instead of Foo(string,object)
Foo f = new Foo("", (int)0); // Uses Foo(string,MyEnum) constructor
instead of Foo(string,object)
BUT
Foo f = new Foo("", 1 ); // Uses Foo(string,object)
Foo f = new Foo("", (object)(int)0 ); // Uses Foo(string,object) with
object type Int32
Foo f = new Foo("", (double)0.0 ); // Uses Foo(string,object)

Actual case was from a ADO.NET Data Provider''s PrvParameter constructors!
Same issue came up resolving the various overloads of
PrvParameterCollection.Add!

Anybody else seen this?
Philippe





Philippe Bertrand <我的***** @ ianywhere.com>在消息中写道

新闻:10 ************* @ corp.supernews.com ...

"Philippe Bertrand" <my*****@ianywhere.com> wrote in message
news:10*************@corp.supernews.com...
这是一个错误吗? C#编译器还是CLR运行时?

枚举MyEnum {ZERO = 0,ONE = 1,TWO = 2}
class Foo {
public Foo(string,object){..公共Foo(字符串,MyEnum){...}
}
Foo f = new Foo("",0); //使用Foo(string,MyEnum)构造函数代替Foo的
(字符串,对象)
Foo f = new Foo("",0.0); //使用Foo(string,MyEnum)构造函数
而不是Foo(字符串,对象)
Foo f = new Foo("",(int)0); //使用Foo(string,MyEnum)构造函数
代替Foo(字符串,对象)
但是Foo f = new Foo(",1); //使用Foo(字符串,对象)
Foo f = new Foo("",(object)(int)0); //使用具有对象类型的Foo(字符串,对象)Int32
Foo f = new Foo("",(double)0.0); //使用Foo(字符串,对象)
Is this a bug in the C# compiler or CLR runtime?

enum MyEnum { ZERO = 0, ONE = 1, TWO = 2 }
class Foo {
public Foo(string,object) { ... }
public Foo(string,MyEnum) { ... }
}

Foo f = new Foo("", 0); // Uses Foo(string,MyEnum) constructor instead of Foo(string,object)
Foo f = new Foo("", 0.0); // Uses Foo(string,MyEnum) constructor instead of Foo(string,object)
Foo f = new Foo("", (int)0); // Uses Foo(string,MyEnum) constructor
instead of Foo(string,object)
BUT
Foo f = new Foo("", 1 ); // Uses Foo(string,object)
Foo f = new Foo("", (object)(int)0 ); // Uses Foo(string,object) with
object type Int32
Foo f = new Foo("", (double)0.0 ); // Uses Foo(string,object)



我再现了它。非常奇怪,尤其是

0.0和(double)0.0之间的行为差​​异,因为0.0已经是双倍。


I reproduced it. Very odd, especially the difference in behavior between
0.0 and (double)0.0, since 0.0 is already a double.


hi philippe,< br $> b $ b见c#语言规范的7.4.2.2。
hi philippe,

see 7.4.2.2 of the C# language specification.
Foo f = new Foo(",0); //使用Foo(string,MyEnum)构造函数代替
Foo(string,object)


在int和MyEnum之间存在隐式转换。

见C#lang规范的6.1.3。

Foo f = new Foo(",",0.0); //使用Foo(string,MyEnum)构造函数而不是Foo(字符串,对象)


在double和int之间存在隐式转换

和MyEnum。见6.1.3。

Foo f = new Foo(",(int)0); //使用Foo(string,MyEnum)构造函数
而不是Foo(string,object)


在int和MyEnum之间存在隐式转换。

见6.1.3。

但是Foo f = new Foo("",1); //使用Foo(字符串,对象)


6.1.3这里不适用。

Foo f = new Foo("", (object)(int)0); //使用Foo(字符串,对象)和对象类型Int32


施放强制Foo(字符串,对象)。

Foo f = new Foo("",(double)0.0); //使用Foo(字符串,对象)
Foo f = new Foo("", 0); // Uses Foo(string,MyEnum) constructor instead of
Foo(string,object)
there exists an implicit convertion between int and MyEnum.
see 6.1.3 of the C# lang specification.
Foo f = new Foo("", 0.0); // Uses Foo(string,MyEnum) constructor instead
of Foo(string,object)
there exists an implicit convertion between double and int
and MyEnum. see 6.1.3.
Foo f = new Foo("", (int)0); // Uses Foo(string,MyEnum) constructor
instead of Foo(string,object)
there exists an implicit convertion between int and MyEnum.
see 6.1.3.
BUT
Foo f = new Foo("", 1 ); // Uses Foo(string,object)
6.1.3 doesn''t apply here.
Foo f = new Foo("", (object)(int)0 ); // Uses Foo(string,object) with
object type Int32
the cast forces Foo(string,object).
Foo f = new Foo("", (double)0.0 ); // Uses Foo(string,object)




强制转换禁用隐式int促销。

因为不存在Foo(字符串,双精度) ,下一个bext

重载方法。


再见

rob



the cast disables the implicit int promotion.
since no Foo(string,double) exists, the next bext
overloaded method is taken.

bye
rob

这篇关于编译器解析为错误的重载构造函数或方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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