接口重新实现从1.0更改为2.0 ??? [英] Interface re-implementation changed from 1.0 to 2.0???
问题描述
关于接口
重新实现,从C#1.0改为2.0。请参阅下面的示例代码;当编译并在VS
2003下运行时,你得到:
Control.Paint
Control.IControl.Paint >
但是当在VS 2005下编译和运行时:你得到:
Control.Paint
Control.Paint >
请解释为什么在C#2.0中,一个重新实现接口的类
更喜欢映射到基类的虚方法而不是显式的
基类的实现。
谢谢
----
使用System;
命名空间InterfaceMapTest
{
class MyApp
{
static void Main (string [] args)
{
MyControl obj = new MyControl();
obj.Paint();
IControl iControl = obj as IControl;
iControl.Paint();
}
}
公共接口IControl
{
无效油漆();
}
class控制:IControl
{
public virtual void Paint()
{
Console.WriteLine(" Control.Paint" );
}
void IControl.Paint()
{
Console.WriteLine( Control.IControl.Paint);
}
}
类MyControl:Control,IControl
{
}
}
Something has changed from C# 1.0 to 2.0 regarding interface
re-implementation. See my sample code below; when compiled and run under VS
2003, you get:
Control.Paint
Control.IControl.Paint
But when compiled and run under VS 2005: you get:
Control.Paint
Control.Paint
Please explain why in C# 2.0, a class that re-implements an interface
prefers to map to a virtual method of the base class rather than an explicit
implementation of the base class.
Thanks
----
using System;
namespace InterfaceMapTest
{
class MyApp
{
static void Main(string[] args)
{
MyControl obj = new MyControl();
obj.Paint();
IControl iControl = obj as IControl;
iControl.Paint();
}
}
public interface IControl
{
void Paint();
}
class Control : IControl
{
public virtual void Paint()
{
Console.WriteLine("Control.Paint");
}
void IControl.Paint()
{
Console.WriteLine("Control.IControl.Paint");
}
}
class MyControl : Control, IControl
{
}
}
推荐答案
已验证...非常奇怪。
" Andy DeMaurice" < an ****** @ noemail.nospamwrote in message
news:04 ************************ ********** @ microsof t.com ...
Verified ... quite weird.
"Andy DeMaurice" <an******@noemail.nospamwrote in message
news:04**********************************@microsof t.com...
关于界面
$ b,从C#1.0变为2.0 $ b重新实施。请参阅下面的示例代码;当编译并运行在
VS
2003之后,你得到:
Control.Paint
Control.IControl.Paint
但是当在VS 2005下编译和运行时:你得到:
Control.Paint
Control.Paint
请解释为什么在C#2.0中,一个重新实现接口的类
更喜欢映射到基础的虚方法类而不是
显式
基类的实现。
谢谢
- -
使用系统;
命名空间InterfaceMapTest
{
类MyApp
{
static void Main(string [] args)
{
MyControl obj = new MyControl();
obj.Paint();
IControl iControl = obj as IControl;
iControl.Paint();
}
}
公共接口IControl
{
void Paint(); < br / >
}
类控制:IControl
{
public virtual void Paint()
{
Console.WriteLine(" Control.Paint");
}
void IControl.Paint( )
{
Console.WriteLine(" Control.IControl.Paint");
}
}
级MyControl:Control,IControl
{
}
}
Something has changed from C# 1.0 to 2.0 regarding interface
re-implementation. See my sample code below; when compiled and run under
VS
2003, you get:
Control.Paint
Control.IControl.Paint
But when compiled and run under VS 2005: you get:
Control.Paint
Control.Paint
Please explain why in C# 2.0, a class that re-implements an interface
prefers to map to a virtual method of the base class rather than an
explicit
implementation of the base class.
Thanks
----
using System;
namespace InterfaceMapTest
{
class MyApp
{
static void Main(string[] args)
{
MyControl obj = new MyControl();
obj.Paint();
IControl iControl = obj as IControl;
iControl.Paint();
}
}
public interface IControl
{
void Paint();
}
class Control : IControl
{
public virtual void Paint()
{
Console.WriteLine("Control.Paint");
}
void IControl.Paint()
{
Console.WriteLine("Control.IControl.Paint");
}
}
class MyControl : Control, IControl
{
}
}
OK ..
我查看了规格并且找不到任何内容快速阅读
通过指定已更改,所以我将其升级到另一个列表。
应该很快就能听到回复。
干杯,
Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
" Andy DeMaurice" < an ****** @ noemail.nospamwrote in message
news:04 ************************ ********** @ microsof t.com ...
OK ..
I checked through the spec and couldn''t find anything on a quick read
through specifying this had changed so I escalated it to another list.
Should hear something back soon.
Cheers,
Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
"Andy DeMaurice" <an******@noemail.nospamwrote in message
news:04**********************************@microsof t.com...
关于界面
$ b,从C#1.0变为2.0 $ b重新实施。请参阅下面的示例代码;当编译并运行在
VS
2003之后,你得到:
Control.Paint
Control.IControl.Paint
但是当在VS 2005下编译和运行时:你得到:
Control.Paint
Control.Paint
请解释为什么在C#2.0中,一个重新实现接口的类
更喜欢映射到基础的虚方法类而不是
显式
基类的实现。
谢谢
- -
使用系统;
命名空间InterfaceMapTest
{
类MyApp
{
static void Main(string [] args)
{
MyControl obj = new MyControl();
obj.Paint();
IControl iControl = obj as IControl;
iControl.Paint();
}
}
公共接口IControl
{
void Paint(); < br / >
}
类控制:IControl
{
public virtual void Paint()
{
Console.WriteLine(" Control.Paint");
}
void IControl.Paint( )
{
Console.WriteLine(" Control.IControl.Paint");
}
}
级MyControl:Control,IControl
{
}
}
Something has changed from C# 1.0 to 2.0 regarding interface
re-implementation. See my sample code below; when compiled and run under
VS
2003, you get:
Control.Paint
Control.IControl.Paint
But when compiled and run under VS 2005: you get:
Control.Paint
Control.Paint
Please explain why in C# 2.0, a class that re-implements an interface
prefers to map to a virtual method of the base class rather than an
explicit
implementation of the base class.
Thanks
----
using System;
namespace InterfaceMapTest
{
class MyApp
{
static void Main(string[] args)
{
MyControl obj = new MyControl();
obj.Paint();
IControl iControl = obj as IControl;
iControl.Paint();
}
}
public interface IControl
{
void Paint();
}
class Control : IControl
{
public virtual void Paint()
{
Console.WriteLine("Control.Paint");
}
void IControl.Paint()
{
Console.WriteLine("Control.IControl.Paint");
}
}
class MyControl : Control, IControl
{
}
}
好的我想我知道发生了什么但是我在等待确认。
我想当你在MyControl上重新实现界面时,它会看到基础上的
Paint()方法,并用它实现接口。我有没有能够找到定义此行为的规范区域
,但我相信这是问题的原因。我将继续通过基于此的规范挖掘
并让你知道我是否可以提出
的东西但是你可以通过不重新实现
派生类上的
接口会产生相同的行为(除了它
有效)
干杯,
Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
" Greg Young" < dr ******************* @ hotmail.comwrote in message
news:uP *********** *** @ TK2MSFTNGP05.phx.gbl ...
OK I think I know what is happenning but I am awaiting confirmation.
I think when you re-implement the interface on MyControl it is seeing the
Paint() method on the base and implementing the interface with it. I have
not been able to find the area of the spec that would define this behavior
but I believe this is the cause of the issue. I will continue digging
through the spec based upon this and let you know if I can come up with
something but immediately you can work around it by not re-implementing the
interface on the derived class which will give the same behavior (except it
works)
Cheers,
Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
"Greg Young" <dr*******************@hotmail.comwrote in message
news:uP**************@TK2MSFTNGP05.phx.gbl...
OK ..
我查看了规格并且没有快速阅读后找不到任何东西
通过指定已更改,所以我将其升级到另一个列表。
应该很快就能听到回复。
干杯,
Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
" Andy DeMaurice" < an ****** @ noemail.nospamwrote in message
news:04 ************************ ********** @ microsof t.com ...
OK ..
I checked through the spec and couldn''t find anything on a quick read
through specifying this had changed so I escalated it to another list.
Should hear something back soon.
Cheers,
Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
"Andy DeMaurice" <an******@noemail.nospamwrote in message
news:04**********************************@microsof t.com...
>有关接口的内容已从C#1.0更改为2.0
重新执行。请参阅下面的示例代码;当编译并运行在
VS
2003下时,你得到:
Control.Paint
Control.IControl.Paint
但是在编译时并在VS 2005下运行:你得到:
Control.Paint
Control.Paint
请解释为什么在C#2.0中,一个重新实现接口的类
更喜欢映射到基类的虚方法而不是基类的实现。
谢谢
----
使用System;
命名空间InterfaceMapTest
{Myote
类
静态无效Main(string [] args)
{
MyControl obj = new MyControl();
obj.Paint();
IControl iControl = obj as IControl;
iControl.Paint();
}
公共接口IControl
{
无效Paint();
}
类控制:IControl
{public void void Paint()
{
Console.WriteLine(" Control.Paint));
}
void IControl.Paint()
{
Console.WriteLine(" Control.IControl.Paint));
}
}
类MyControl:控制,IControl
>Something has changed from C# 1.0 to 2.0 regarding interface
re-implementation. See my sample code below; when compiled and run under
VS
2003, you get:
Control.Paint
Control.IControl.Paint
But when compiled and run under VS 2005: you get:
Control.Paint
Control.Paint
Please explain why in C# 2.0, a class that re-implements an interface
prefers to map to a virtual method of the base class rather than an
explicit
implementation of the base class.
Thanks
----
using System;
namespace InterfaceMapTest
{
class MyApp
{
static void Main(string[] args)
{
MyControl obj = new MyControl();
obj.Paint();
IControl iControl = obj as IControl;
iControl.Paint();
}
}
public interface IControl
{
void Paint();
}
class Control : IControl
{
public virtual void Paint()
{
Console.WriteLine("Control.Paint");
}
void IControl.Paint()
{
Console.WriteLine("Control.IControl.Paint");
}
}
class MyControl : Control, IControl
{
}
}
这篇关于接口重新实现从1.0更改为2.0 ???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!