我有一个 BookmarkCollection 类,它是 Aspose 单词文档生成器中使用的 IEnumerable 继承,我必须检查要从此方法提供的 docx 文件中的每个硬编码书签。我需要检查它是否为空才能填写书签。考虑到收藏中有很多书签,我正在寻找一种更好的方法来做到这一点。
private void FillBookmarks(long AppID, BookmarkCollection bs)
{
if (bs["Bookmark1"] != null)
{
//fetch data fill it in the docx file for Bookmark1
}
if (bs["Bookmark2"] != null)
{
//fetch data fill it in the docx file for Bookmark2
}
if (bs["Bookmark3"] != null)
{
//fetch data fill it in the docx file for Bookmark3
}
//if statements goes on and on
}
考虑到很多可能的 if 语句添加,我该如何改进
BookmarkCollection 是这样的:
public class BookmarkCollection : IEnumerable
{
public int Count { get; }
public Bookmark this[int index] { get; }
public Bookmark this[string bookmarkName] { get; }
public void Clear();
public IEnumerator GetEnumerator();
public void Remove(Bookmark bookmark);
public void Remove(string bookmarkName);
public void RemoveAt(int index);
}
提供有关书签的更多信息以及如何处理它们:Bookmarks1、2、3 是要检索的不同类型的数据,例如价格、客户地址、产品名称、供应商等。当集合具有字符串即“供应商”时,该do something
方法将从数据库中检索数据。