我收到`System.InvalidOperationException:收集被修改;枚举操作可能无法执行,莫名其妙

时间:2013-04-23 18:28:13

标签: c# .net ienumerable

我正在System.InvalidOperationException: Collection was modified; enumeration operation may not execute

ExceptionLoggingLibrary.LoggingException: Exception of type 'ExceptionLoggingLibrary.LoggingException' was thrown. ---> System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at iTextSharp.text.FontFactoryImp.GetFont(String fontname, String encoding, Boolean embedded, Single size, Int32 style, BaseColor color, Boolean cached)
[...]

根据我的理解,在枚举期间修改IEnumerable对象时会发生异常。

以下是iTextSharp.text.FontFactoryImp.GetFont方法:

    public virtual Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached) {
        if (fontname == null) return new Font(Font.FontFamily.UNDEFINED, size, style, color);
        string lowercasefontname = fontname.ToLower(CultureInfo.InvariantCulture);
        List<string> tmp;
        fontFamilies.TryGetValue(lowercasefontname, out tmp);
        if (tmp != null) {
            // some bugs were fixed here by Daniel Marczisovszky
            int fs = Font.NORMAL;
            bool found = false;
            int s = style == Font.UNDEFINED ? Font.NORMAL : style;
            foreach (string f in tmp) {
                string lcf = f.ToLower(CultureInfo.InvariantCulture);
                fs = Font.NORMAL;
                if (lcf.ToLower(CultureInfo.InvariantCulture).IndexOf("bold") != -1) fs |= Font.BOLD;
                if (lcf.ToLower(CultureInfo.InvariantCulture).IndexOf("italic") != -1 || lcf.ToLower(CultureInfo.InvariantCulture).IndexOf("oblique") != -1) fs |= Font.ITALIC;
                if ((s & Font.BOLDITALIC) == fs) {
                    fontname = f;
                    found = true;
                    break;
                }
            }
            if (style != Font.UNDEFINED && found) {
                style &= ~fs;
            }
        }
        BaseFont basefont = null;
        try {
            try {
                // the font is a type 1 font or CJK font
                basefont = BaseFont.CreateFont(fontname, encoding, embedded, cached, null, null, true);
            }
            catch (DocumentException) {
            }
            if (basefont == null) {
                // the font is a true type font or an unknown font
                trueTypeFonts.TryGetValue(fontname.ToLower(CultureInfo.InvariantCulture), out fontname);
                // the font is not registered as truetype font
                if (fontname == null) return new Font(Font.FontFamily.UNDEFINED, size, style, color);
                // the font is registered as truetype font
                basefont = BaseFont.CreateFont(fontname, encoding, embedded, cached, null, null);
            }
        }
        catch (DocumentException de) {
            // this shouldn't happen
            throw de;
        }
        catch (System.IO.IOException) {
            // the font is registered as a true type font, but the path was wrong
            return new Font(Font.FontFamily.UNDEFINED, size, style, color);
        }
        catch {
            // null was entered as fontname and/or encoding
            return new Font(Font.FontFamily.UNDEFINED, size, style, color);
        }
        return new Font(basefont, size, style, color);
    }

在该方法中,枚举期间是否可能修改了IEnumerable对象?

1 个答案:

答案 0 :(得分:8)

如果不知道方法中的内容,这将阻止在枚举期间更改Collection:

变化:

List<string> tmp;
fontFamilies.TryGetValue(lowercasefontname, out tmp);

要:

List<string> sharedList;
fontFamilies.TryGetValue(lowercasefontname, out sharedList);
var tmp = new List<string>(sharedList);

这将为您提供一个新列表,您可以确定其他任何线程都无法访问该列表,因为它保证不仅仅是对TryGetValue()中列表的引用。

我已经从之前更改了tmp列表的名称,并将新列表命名为tmp,这样您就不需要更改任何其他代码了。