为什么IntelliSense在foreach循环中不能按预期工作?

rle*_*ias 1 c# intellisense visual-studio

在输入以下代码片段时,我注意到Intellisense没有按预期工作:

StringBuilder sb = new StringBuilder();

foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(sb))
{
    var name = prop.DisplayName;
    Console.WriteLine("{0}", name);
}
Run Code Online (Sandbox Code Playgroud)

在foreach语句中,如果我开始输入prop.Di,则Intellisense将以prop.DisplayName完成.但是,我使用var关键字代替PropertyDescriptor,然后我只看到从object继承的方法.

TypeDescriptor.GetProperties()返回TypeDescriptor的集合时,我认为Visual Studio能够推断出正确的类型prop.

为什么不起作用?

Lee*_*Lee 8

GetProperties返回PropertyDescriptorCollection只实现的IEnumerable,而不是IEnumerable<PropertyDescriptor>.如果使用var,prop则推断类型object不是PropertyDescriptor.