2017-02-21 75 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的前泛型方法。