0

I am building a VSIX package to support a custom language in Visual Studio using MPF. I am in a custom designer and I need to find the files referenced in the project to resolve some dependencies. Where can I access this list?

4

1 回答 1

1

我假设您正在使用MPF为您的自定义语言服务实现项目系统。这样做时,您可能有一个项目根节点,它派生自 ProjectNodeHierarchyNode...

如果是这样,您可以与设计者共享根节点的实例,并尝试通过遍历层次结构来查找文件,例如...

internal class HierarchyVisitor
{
    private readonly Func<HierarchyNode, bool> filterCallback;

    public HierarchyVisitor(
        Func<HierarchyNode, bool> filter)
    {
        this.filterCallback = filter;
    }

    public IEnumerable<HierarchyNode> Visit(
        HierarchyNode node)
    {
        var stack = new Stack<HierarchyNode>();
        stack.Push(node);
        while (stack.Any())
        {
            HierarchyNode next = stack.Pop();
            if (this.filterCallback(next))
            {
                yield return next;
            }

            for (
                HierarchyNode child = next.FirstChild; 
                child != null; 
                child = child.NextSibling)
            {
                stack.Push(child);
            }
        }
    }
}

要获取层次结构中所有节点的列表,您可以这样做...

ProjectNode root = ...
var visitor = new HierarchyVisitor(x => true);
IEnumerable<HierarchyNode> flatList = visitor.Visit(root);

或者要过滤特定的文件类型,你可以尝试这样的事情......

ProjectNode root = ...
var visitor = new HierarchyVisitor((HierarchyNode x) => 
    {
        const string XmlFileExtension = ".xml";
        string path = new Uri(x.Url, UriKind.Absolut).LocalPath;
        return string.Compare(
            XmlFileExtension, 
            Path.GetFileExtension(path), 
            StringComparison.InvariantCultureIgnoreCase) == 0;
    });

IEnumerable<HierarchyNode> xmlFiles = visitor.Visit(root);
于 2015-03-13T09:35:06.493 回答