Bagikan melalui


Overview of the CodeModel Object for Visual Basic and C# Applications

You may need to programmatically examine the structure of your code in a Visual Basic or Visual C# application. Perhaps you would like to know the namespaces defined in your application and their nesting relationships. Alternatively, you might want to create a document that lists the classes and their public members as a basis for documentation.

The CodeModel2 object is a general extensibility object that provides access to the code in an application. The CodeModel2 object does not provide a textual list of the code. Instead, it provides a nested collection of objects that represents code elements. For example, each namespace in the code is represented by a CodeNamespace object. The CodeModel2 object associated with a project is accessed by the CodeModel property.

The FileCodeModel2 object is a general extensibility object that provides access to the code in a single source file. Each source file in an application is represented by a ProjectItem object, and the FileCodeModel2 object associated with a project item is found in the FileCodeModel property.

Both the CodeModel2 and the FileCodeModel2 objects provide a CodeModel2.CodeElements and FileCodeModel.CodeElements property, respectively, that is a collection of CodeElement2 objects. Visual Basic and Visual C# support the CodeModel2 and FileCodeModel objects in slightly different ways. For an example of a Visual Basic implementation, see Discovering Code by Using the Code Model (Visual Basic) and How to: Use the CodeModel Object to Analyze Visual Basic Code. For an example of a Visual C# implementation, see Discovering Code by Using the Code Model (Visual C#) and How to: Create a C# Class by Using the CodeModel Object.

Some languages do not implement the entire Visual Studio code model. Help topics point out exceptions when they exist. Most differences between implementations of the code model are due to functional differences between the languages. For example, you cannot add functions to CodeNamespace objects in Visual Basic or Visual C# because only Visual C++ features top-level function definitions.

The main code model objects are listed in the following table. Click the links to see the list of methods and properties for each object.

Type

Description

CodeNamespace

Represents a namespace construct in a source file.

CodeStruct2

Represents a structure in source code.

CodeInterface2

Represents an interface in source code.

CodeClass2

Represents a class in source code.

CodeEnum

Represents an enumeration in source code.

CodeVariable2

Defines a variable construct in a source file.

CodeDelegate2

Represents a delegate in source code.

CodeElement2

Represents a code element or construct in a source file.

CodeEvent

Models a code event element.

CodeProperty2

Defines a property construct in a source file.

CodeAttribute2

Defines an attribute for a code element.

CodeImport

Models the use of statements that import namespaces.

CodeFunction2

Represents a function construct in a source file.

CodeParameter2

Defines a parameter to a function, property, and so on.

Nested Code Elements

Code elements in an application are organized in a nested architecture. For example, the FileCodeModel2 of a project item may contain, in its CodeElements collection, a code element for a namespace. The CodeNamespace object representing that namespace may contain several other code elements that representing classes in its Members property. Each CodeClass2 element, in turn, may contain code elements representing properties (CodeProperty2 object) and methods (CodeFunction2 object). The methods for retrieving nested code elements depend on the type of code element and are described in the next section.

Code Element Categories

The CodeElement2 class is a base class that represents all code elements in a project item (source file). Many classes derive from CodeElement2 to represent the constructs in an application, including namespaces, classes, properties, methods, and events. There are two expansive categories of code elements — those that represent types and those that do not.

Some code elements in an application represent types. These code elements derive from the CodeType object as well as the CodeElement2 object:

You can determine if a reference to a CodeElement2 object is one of these types by using the IsCodeType property. To find the nested code elements of these types, use the Members property, which returns a CodeElements collection. How to: Use the CodeModel Object to Analyze Visual Basic Code includes an example that finds all the classes in a project.

How to find code elements nested under other types of code elements depends on the type of the nested code element. For example, because a property has only a Get and a Set method, the CodeProperty2 object associated with a property has Getter and Setter properties that return CodeFunction2 elements. The following list shows which properties to use to find the nested code elements of the code element types.

Type

Property

CodeFunction

Parameters

CodeNamespace

Members

CodeProperty

Getter and Setter

Code Elements from External References

Your code may contain type declarations from types defined in project references. (Project references are added through the Add References dialog box or with the References.Add extensibility method.) The Visual Basic project system does not resolve the CodeTypeRef2 objects representing these references beyond their names. Therefore, if you try to use any of the other properties or methods of the CodeElement2 object associated with the reference, other than the Name property, a "Not Implemented" exception will be thrown. These references are referred to as "type-name only" code elements. CodeType objects that represent external references have the InfoLocation property set to vsCMInfoLocationNone.

Compiler Errors and the CodeModel Object

When you are writing code that maintains references to CodeElement2 objects, you should be aware that the underlying source code can change while you are holding the reference. The code element may be deleted, renamed, or involved in a compiler error. When this happens, calls to the CodeElement2 object return the error message, "Exception from HRESULT: 0x80047E2C."

See Also

Tasks

How to: Use the CodeModel Object to Analyze Visual Basic Code

Troubleshooting Visual Basic and Visual C# Extensibility

Concepts

Discovering Code by Using the Code Model (Visual Basic)

Discovering Code by Using the Code Model (Visual C#)