Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在VisualStudio2008外接程序中,如何判断类';属性实现?_C#_Visual Studio 2008_Vsx_Visual Studio Addins - Fatal编程技术网

C# 在VisualStudio2008外接程序中,如何判断类';属性实现?

C# 在VisualStudio2008外接程序中,如何判断类';属性实现?,c#,visual-studio-2008,vsx,visual-studio-addins,C#,Visual Studio 2008,Vsx,Visual Studio Addins,在VisualStudio外接程序中,我正在枚举当前源文件中类的成员。当我遇到一个属性(例如codelement.Kind==vsCMElement.vsCMElementProperty)时,我将该codelement转换为一个CodeProperty,我可以看到该属性的名称和类型 我遇到的问题是获取属性的实现接口列表。我想知道这是否是因为实现的接口可能在VisualStudio“不知道”的程序集中 有没有办法获取属性实现的接口列表 谢谢。是的。您必须确定该属性是类(CodeClass)还是接

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

我遇到的问题是获取属性的实现接口列表。我想知道这是否是因为实现的接口可能在VisualStudio“不知道”的程序集中

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


谢谢。

是的。您必须确定该属性是类(CodeClass)还是接口(CodeInterface)。在这两种情况下,您都需要迭代所有代码(类/接口).base并递归检查ImplementedInterface

下面是一些示例代码(注意:这只是为了帮助理解这个想法)


谢谢你的回复。我试图为其获取实现接口的元素是一个属性(例如codelement.Kind==vsCMElement.vsCMElementProperty),因此我认为我没有编解码器类。您知道如何从CodeElement对象获取CodeClass对象吗?更新了一个更全面的示例。

        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
            }
        }