2017-02-21 73 views
1

我们循环这个方法切换到AWS我得到一个奇怪的错误后,现在的工作:通过字典项iTextSharp的

public static Dictionary<string, string> GetFormFieldNames(string pdfPath) 
{ 
    var fields = new Dictionary<string, string>(); 

    foreach (DictionaryEntry entry in reader.AcroFields.Fields) 
    { 
     fields.Add(entry.Key.ToString(), string.Empty); 
    } 

    return fields; 
} 

Cannont convert type System.Collections.Generic.KeyValuePair<String.iTextSharp.text.pdf.AcroFields.item> to System.CollectionsEntry

我理解错误是显而易见的,但我似乎无法得到输入正确的。为什么这种方法停止工作?我在这里做错了什么?

+1

你可以使用var(代替DictionaryEntry),然后使用intellisense在你的字典中得到正确的值吗? – thinklarge

+1

@thinklarge我做了你的建议,并表示感谢!如果您将其作为答案,我会将其标记为已回答。我无法想象现在有多少个itext用户可能会遇到这个问题。 – MizAkita

+1

感谢您的回复。出于这个原因,我喜欢C#的工具。 Intellisense使新库的学习曲线变得更加容易。 – thinklarge

回答

1

您可以使用var而不是类型定义,然后为您完成Intellisence。

public static Dictionary<string, string> GetFormFieldNames(string pdfPath) 
{ 
    var fields = new Dictionary<string, string>(); 

    foreach (var entry in reader.AcroFields.Fields) 
    { 
     fields.Add(entry.*use intellisense here*, string.Empty); 
    } 

    return fields; 
} 
0

错误消息没有意义。一个KeyValuePair有两个类型参数,但是你发布的消息只有一个。在.NET中也没有System.CollectionsEntry类型或命名空间,所以我怀疑你截断了你的实际错误信息。在任何情况下,我猜字段的类型是一个System.Collections.Generic.KeyValuePair <字符串,iTextSharp.text.pdf.AcroFields.Item >。你可以使用foreach(reader.AcroFields.Fields中的var kvp)。

至于为什么事情“改变了”,也许你改变了.NET版本? DictionaryEntry是迭代Hashtables的前泛型方法。