2013-06-04 41 views
-1
using System.Reflection; 

internal class TestReflection 
{ 
    public static IEnumerable<object> ParseType(Type t) 
    { 
     foreach (MemberInfo mi in t.GetMembers()) 
     { 
      yield return mi; 
      foreach (object x in mi.GetCustomAttributes(true)) 
      { 
       yield return x; 
      } 
     } 
    } 
} 

所以我的問題是如何迭代器知道什麼時候進入嵌套的foreach。我曾假設第一個收益回報總是會執行,並且嵌套收益回報永遠不會執行。但事實並非如此,這表明我對收益回報聲明並不太瞭解。有人可以解釋爲什麼嵌套的收益率執行(C#收益率回報)嗎?

有人可以解釋這一點嗎?

感謝 詹姆斯

+0

這可能會給你一些**光(或不,可能不是最好的解釋):http://blog.alxandr.me/2012/10/10/yielding-results-the-process-infolved -with產生髮電機/ – Alxandr

回答

0

如果只有一個元素從枚舉的要求,那麼你是對的,但如果要求的元素,則內foreach必須執行,以產生更多的元素。