views:

53

answers:

1

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.

+1  A: 

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
            }
        }
CDSO1
Thanks for your response. The element I'm trying to get implemented interfaces for is a property (e.g. CodeElement.Kind == vsCMElement.vsCMElementProperty) so I don't think I have a CodeClass. Do you know how to get a CodeClass object from a CodeElement object?
Tim Stewart
Updated with a more encompassing example.
CDSO1