Correct way to check if IEnumerable is created by a yield keyword

后端 未结 2 1367
余生分开走
余生分开走 2020-12-11 08:25

What is the correct way to check if an IEnumerable is generated by the yield keyword ?

Sample :

public IEnumerable

        
2条回答
  •  我在风中等你
    2020-12-11 09:09

    I wouldn't necessarily say it's the correct way, but A way is:

    someInts.GetType().Name.Contains("")
    

    This is probably fairly reliable since this name is very abnormal. Since you're relying on deep internals it could change between any .NET release, although i feel it probably wouldn't/hasn't.

    If you use this, make sure to keep a unit test to sanity check it. Such that if the internals do change, your unit test will fail on the build server.

提交回复
热议问题