在 Visual Studio 2008 插件中,如何判断类的接口是哪些?财产工具?

发布于 2024-08-28 16:16:58 字数 272 浏览 5 评论 0原文

在 Visual Studio 外接程序中,我枚举当前源文件中的类的成员。当我遇到一个属性(例如 CodeElement.Kind == vsCMElement.vsCMElementProperty)时,我将该 CodeElement 转换为 CodeProperty,并且可以看到该属性的名称和类型。

我遇到的问题是获取属性的已实现接口的列表。我想知道这是否是因为实现的接口可能位于 Visual Studio 不“了解”的程序集中。

有没有办法获取属性实现的接口列表?

谢谢。

In a Visual Studio Add-In, I'm enumerating over the members of a class in the current source file. When I encounter a property (e.g. CodeElement.Kind == vsCMElement.vsCMElementProperty) I cast that CodeElement to a CodeProperty and I can see the property's name and type.

What I'm having a problem with is getting a list of the property's implemented interfaces. I'm wondering if this is because the implemented interfaces might be in assemblies that Visual Studio doesn't "know" about.

Is there a way to get the list of interfaces that a property implements?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃兔兔 2024-09-04 16:16:58

是的。您必须确定该属性是类 (CodeClass) 还是接口 (CodeInterface)。无论哪种情况,您都需要迭代所有代码(类/接口).Bases 并递归检查 ImplementedInterfaces。

这是一些示例代码(注意:这只是为了帮助实现这个想法)


        private void ProcessDocument()
        {
            CodeElements elements = _applicationObject.ActiveDocument.ProjectItem.FileCodeModel.CodeElements;
            foreach (CodeElement element in elements)
            {
                if (element.Kind == vsCMElement.vsCMElementNamespace)
                {

                    CodeNamespace ns = (CodeNamespace)element;
                    foreach (CodeElement elem in ns.Members)
                    {
                        if (elem is CodeClass)
                        {
                            CodeClass cls = elem as CodeClass;
                            foreach (CodeElement member in cls.Members)
                                if (member is CodeProperty)
                                {
                                    CodeType memberType = ((member as CodeProperty)).Type.CodeType;
                                    ProcessElem(memberType as CodeElement);
                                }
                        }
                    }
                }
            }
        }

        private void ProcessElem(CodeElement elem)
        {
            if (null == elem) return;
            // we only care about elements that are classes or interfaces.
            if (elem is CodeClass)
            {

                CodeClass cls = elem as CodeClass;
                CodeElements intfs = cls.ImplementedInterfaces;

                // do whatever with intfs
                // ...

                CodeElements bases = cls.Bases;
                foreach (CodeElement baseElem in bases)
                    ProcessElem(baseElem);
            } 
            else if (elem is CodeInterface)
            {
                // same as class, figure out all other interfaces this interface 
                // derives from if needed
            }
        }

Yes. You would have to determine if the property is a Class (CodeClass) or an Interface (CodeInterface). In either case, you will need to iterate through all the Code(Class/Interface).Bases and recursively check ImplementedInterfaces.

Here is some example code (note: this is just to help with the idea)


        private void ProcessDocument()
        {
            CodeElements elements = _applicationObject.ActiveDocument.ProjectItem.FileCodeModel.CodeElements;
            foreach (CodeElement element in elements)
            {
                if (element.Kind == vsCMElement.vsCMElementNamespace)
                {

                    CodeNamespace ns = (CodeNamespace)element;
                    foreach (CodeElement elem in ns.Members)
                    {
                        if (elem is CodeClass)
                        {
                            CodeClass cls = elem as CodeClass;
                            foreach (CodeElement member in cls.Members)
                                if (member is CodeProperty)
                                {
                                    CodeType memberType = ((member as CodeProperty)).Type.CodeType;
                                    ProcessElem(memberType as CodeElement);
                                }
                        }
                    }
                }
            }
        }

        private void ProcessElem(CodeElement elem)
        {
            if (null == elem) return;
            // we only care about elements that are classes or interfaces.
            if (elem is CodeClass)
            {

                CodeClass cls = elem as CodeClass;
                CodeElements intfs = cls.ImplementedInterfaces;

                // do whatever with intfs
                // ...

                CodeElements bases = cls.Bases;
                foreach (CodeElement baseElem in bases)
                    ProcessElem(baseElem);
            } 
            else if (elem is CodeInterface)
            {
                // same as class, figure out all other interfaces this interface 
                // derives from if needed
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文