c# - 'yield return' 语句不能出现在 try/catch block 约束中

标签 c#

<分区>

这两种方法实际上是一样的,但第一种无法编译。我想不通这个约束存在的原因

    /// <summary>
    /// Dynamically loads all document extractors from implementation assemblies into an enumeration
    /// </summary>
    private static IEnumerable<IDocumentExtractor> EnumerateInstances()
    {
        IEnumerable<Type> types = EnumerateTypes();

        foreach(Type type in types)
        {
            try
            {
                IDocumentExtractor extractor = Activator.CreateInstance(type) as IDocumentExtractor;
                yield return extractor;
            }
            catch
            {
                _log.WarnFormat("Type {0} couldn't be instanced.", type.Name);
            }
        }
    }

以及实际编译没有问题的版本:

    /// <summary>
    /// Dynamically loads all document extractors from implementation assemblies into an enumeration
    /// </summary>
    private static IEnumerable<IDocumentExtractor> EnumerateInstances()
    {
        IEnumerable<Type> types = EnumerateTypes();

        foreach (Type type in types)
        {
            IDocumentExtractor extractor = null;
            try
            {
                extractor = Activator.CreateInstance(type) as IDocumentExtractor;
            }
            catch
            {
                _log.WarnFormat("Type {0} couldn't be instanced.", type.Name);
            }

            if (extractor != null)
                yield return extractor;
        }
    }

最佳答案

Eric Lippert 在他的 blog series about iterator blocks 中详细解释了这一点.从底部开始逐步向上,在到达正确位置之前不要跳过任何内容。

除此之外我不会尝试解释 - 但我会引用 post 5 的一部分,它实际上是谈论 try block 的 block ,它有 catch block (post 4 谈论 catch block 本身,但这是另一回事).

So what if the try block has a catch?

The original designers of C# 2.0 -- and remember, this is long before my time on the team -- had a huge debate over this. A debate which was repeated in miniature when I sent them all an email asking for the justification for this decision. There are three basic positions:

  1. Do not allow yield returns in try blocks at all. (Or blocks that are sugars for try blocks, like using blocks.) Use some other mechanism other than "finally" to express the idea of "this is the code that you run when the caller shuts down the enumeration."

  2. Allow yield returns in all try blocks.

  3. Allow yield returns in try blocks that have finally blocks, but not if they have catch blocks.

[snip]

The down side of (3) is that the rule seems arbitrary and weird -- until you read five unnecessarily prolix blog entries that explain what on earth the design team was thinking.

Obviously, they picked (3), and now you know why.

关于c# - 'yield return' 语句不能出现在 try/catch block 约束中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7996481/

相关文章:

c# - '@' 在 C# 中做什么?

c# - parallel.foreach 和 httpclient - 奇怪的行为

c# - 使用泛型类和接口(interface)实现抽象工厂

c# - 无法将 Microsoft Tag WebService 引用添加到 MVC 4 项目

c# - 通过 REST API 创建 B2C 租户返回 401/未经授权

c# - 为什么不能递归调用Worker_DoWork函数?

c# - NHibernate 自动将我的对象更新到数据库,而无需在 Asp.Net MVC 中调用 SaveOrUpdate

c# - C# 中的多态性和静态方法重载。

c# - 无法访问已处置的对象 Asp.net Identity Core

c# - 使用 Asp.net 连接 MySql 时出错