Search Wiki:
ClariusLabsCodePlex.png

Introduction

LinqToCodeModel is a sample library that provides Linq facilities over FileCodeModel object graphs. It is based on CodeElement iterators and extension methods.

For more information on VSX, visit the VSX Developer Center at http://msdn.com/vsx.

Links

Releases1.0 release
Pre-requisites

Target audience

  • VSX developers
  • Visual Studio enthusiasts

Samples


Locate classes and print names

 
            FileCodeModel fcm = dte.ActiveDocument.ProjectItem.FileCodeModel;
 
            foreach(CodeElement element in fcm.CodeElements)
            {
                if(element is CodeNamespace)
                {
                    CodeNamespace @namespace = element as CodeNamespace;
 
                    foreach(CodeElement subElement in @namespace.Children)
                    {
                        if(subElement is CodeClass)
                        {
                            CodeClass @class = subElement as CodeClass;
 
                            Debug.WriteLine(@class.Name);
 
                            //Inner classes are not considered here
                        }
                    }
                }
            }

Locate classes and print names using Linq

            FileCodeModel fcm = dte.ActiveDocument.ProjectItem.FileCodeModel;
 
            fcm.GetIEnumerable<CodeClass>()
               .ForEach(@class => Debug.WriteLine(@class.Name));

Locate a class with a "Foo" name

            FileCodeModel fcm = dte.ActiveDocument.ProjectItem.FileCodeModel;
 
            foreach(CodeElement element in fcm.CodeElements)
            {
                if(element is CodeNamespace)
                {
                    CodeNamespace @namespace = element as CodeNamespace;
 
                    foreach(CodeElement subElement in @namespace.Children)
                    {
                        if(subElement is CodeClass)
                        {
                            CodeClass @class = subElement as CodeClass;
 
                            if(@class.Name.Equals("Foo"))
                            {
                                break;
                            }
 
                            //Inner classes are not considered here
                        }
                    }
                }
            }

Locate a class with a "Foo" name using Linq

            FileCodeModel fcm = dte.ActiveDocument.ProjectItem.FileCodeModel;
 
            fcm.GetIEnumerable<CodeClass>()
                .SingleOrDefault(@class => @class.Name.Equals("Foo")); 

More Samples


Getting started


This project is sponsored by Clarius Labs
Last edited Mar 28 2008 at 6:50 PM  by pablogaliano, version 10
Updating...
Page view tracker