开发者

Within a Visual Studio 2008 Add-In, how can I tell which interfaces a class' property implements?

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 lis开发者_Python百科t of interfaces that a property implements?

Thanks.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜