Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 为什么类型推断逻辑在TreeNodeCollection上的迭代失败?_.net_Winforms_Visual Studio 2013_Type Inference_C# 5.0 - Fatal编程技术网

.net 为什么类型推断逻辑在TreeNodeCollection上的迭代失败?

.net 为什么类型推断逻辑在TreeNodeCollection上的迭代失败?,.net,winforms,visual-studio-2013,type-inference,c#-5.0,.net,Winforms,Visual Studio 2013,Type Inference,C# 5.0,我对这种行为有点困惑 这段代码编译得很好: private void CheckAvailabilityOfCurrentTreeNodes() { foreach( TreeNode treeNode in this.DeviceTree.Nodes ) { if( Object.ReferenceEquals(treeNode.Tag, _recorder.CaptureDevice) ) { ... } } } 但是下面生

我对这种行为有点困惑

这段代码编译得很好:

private void CheckAvailabilityOfCurrentTreeNodes() {
   foreach( TreeNode treeNode in this.DeviceTree.Nodes ) {
      if( Object.ReferenceEquals(treeNode.Tag, _recorder.CaptureDevice) ) {
         ...     
       }
   }
}
但是下面生成一个编译错误,说明
'object'不包含“Tag”的定义

private void CheckAvailabilityOfCurrentTreeNodes() {
   foreach( var treeNode in this.DeviceTree.Nodes ) {
      if( Object.ReferenceEquals(treeNode.Tag, _recorder.CaptureDevice) ) {
         ...     
       }
   }
}
在本例中,类型推断逻辑返回
对象
作为迭代器的结果类型,但我不清楚为什么,因为
树集合
是强类型的。是因为这种收藏类型已经过时了吗

有人能告诉我这种行为的技术原因吗?

因为
TreeNodeCollection
是强类型的

以下是该类的定义:

如您所见,它不是强类型的,因为它实现了非泛型的
IEnumerable
接口,而不是
IEnumerable
。因此,类型推断是有效的,只是从
IEnumerable.Current
推断出的类型是
object

这基本上适用于所有WinForms集合。尽管它们大多数都提供强类型索引器,但它们都实现了非泛型的
IEnumerable
接口


为什么??因为它们是在泛型(因此
IEnumerable
interface)不存在的时候创建的。而且永远不会更新。

事实上,该系列的强类型“facade”让我相信它某种程度上是强类型的。谢谢你的洞察力!
public class TreeNodeCollection : IList, ICollection, IEnumerable