将书签集合转换为列表
我在现有的 Word 文档中获取书签,如下所示
using System.Collections.Generic;
using Word = Microsoft.Office.Interop.Word;
namespace Word_Project
{
public class WordDoc
{
public Word.Application App { get; set; }
public Word.Document Document { get; set; }
public Word.Bookmarks Bookmarks { get; set; }
public WordDoc(string pathToDoc)
{
App = new Word.Application();
Document = App.Documents.Open(pathToDoc);
Bookmarks = Document.Bookmarks;
}
}
}
,书签签名是
public interface Bookmarks : System.Collections.IEnumerable
我想将其转换为列表<> (系统.集合.通用) 书签不包含 .ToList() 方法,并且转换也不起作用。
(List<Bookmark>)Bookmarks //error
也许一开始就不可能,因为书签是一个界面,但如果有人有解决方案,我将不胜感激。
I get the bookmarks in an existing Word-Document as follows
using System.Collections.Generic;
using Word = Microsoft.Office.Interop.Word;
namespace Word_Project
{
public class WordDoc
{
public Word.Application App { get; set; }
public Word.Document Document { get; set; }
public Word.Bookmarks Bookmarks { get; set; }
public WordDoc(string pathToDoc)
{
App = new Word.Application();
Document = App.Documents.Open(pathToDoc);
Bookmarks = Document.Bookmarks;
}
}
}
the Signature of Bookmarks is
public interface Bookmarks : System.Collections.IEnumerable
I'd like to convert this to a List<> (System.Collections.Generic)
Bookmarks doesn't contain a .ToList()-Method and casting didn't work either.
(List<Bookmark>)Bookmarks //error
Maybe it isn't possible in the first place, as Bookmarks is an Interface, but if someone has a solution I would appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我仅从文档中回答,因为我没有 Office,因此无法使用 Interop。
似乎 Microsoft.Office.Interop.Word.Bookmarks 仅公开 GetEnumerator 方法,因此您也不能使用 lambda foreach ->选择方法。
那么我提出一个非常通用的解决方案。
然后调用如下:
你可以使用yield return而不是返回一个列表,但OP要求一个列表
I'm answering only from docs because I havent Office and so, can't use Interop.
Seems that Microsoft.Office.Interop.Word.Bookmarks only exposes the GetEnumerator method, so you also cant go for a lambda foreach -> select aproach.
Well then I propose a very generic solution.
Then call like:
you can use yield return instead returning a list, but OP asked for a List