与访问Microsoft.Office.Core.DocumentProperties性能问题

我有一个Excel COM插件读取工作簿的CustomDocumentProperties部分。

这就是我如何从CustomDocumentProperties部分访问特定的条目

DocumentProperties docProperties = (DocumentProperties) xlWorkbook.CustomDocumentProperties; docProperty = docProperties[propName]; 

问题是当CustomDocumentProperties包含超过8000个条目时,这个代码的性能非常糟糕。 我已经运行CPU分析器,它显示下面的行花费了一分多钟。

 docProperty = docProperties[propName]; 

有谁知道如何提高访问DocumentProperties的性能?

谢谢!

我怀疑你有什么可以改进文档属性的性能。 我相信它是作为一个简单的列表来实现的,而不是作为一个字典或散列表。 事实上,我不认为这个清单是有序的,所以8000个条目,平均一半,或4000,将不得不被访问,以find你正在寻找的财产。

您可能会考虑不使用CustomDocumentProperties作为字典。 相反,您可以尝试将全部8000个条目放入自定义字典中,对其进行序列化,然后将整个序列化字典作为单个条目添加到CustomDocumentProperties中。 因此,要使用它,您将访问CustomDocumentProperties,反序列化字典,然后重复使用它。 完成之后,如果对字典有任何更改,则必须重新序列化该字典,并将其保存回CustomDocumentProperties,您可能只需要执行一次,例如在保存工作簿之前。 (您可能希望将代码重新序列化并将自定义字典保存到Workbook.BeforeSave事件中的CustomDocumentProperties中。)