Code Model Objects
This section explains the workings of the code model objects and their properties. We'll defer an examination of the code model methods until the section titled "Generating Code."
FileCodeModel and CodeModel
The FileCodeModel object and the CodeModel object are the two entryways into the code model; each contains a collection of top-level code elements in addition to methods that allow you to add, delete, and modify those code elements. Table A-1 lists the FileCodeModel properties.
Property | Description |
---|---|
CodeElements | Returns a collection of top-level CodeElement objects defined in the associated source file |
DTE | Returns the DTE object |
Language | Returns a GUID representing the source file's programming language |
Parent | Returns the ProjectItem object for the associated source file |
string LanguageFromGUID(string langGuid)
{
string language = String.Empty;
switch (langGuid.ToUpper())
{
case CodeModelLanguageConstants.vsCMLanguageCSharp:
language = "Visual C#";
break;
case CodeModelLanguageConstants.vsCMLanguageVB:
language = "Visual Basic";
break;
case "{E6FDF8BF-F3D1-11D4-8576-0002A516ECE8}":
language = "Visual J#";
break;
case CodeModelLanguageConstants.vsCMLanguageVC:
language = "Visual C++";
break;
default:
language = "Other";
break;
}
return language;
}
ImportantYou can't just open an arbitrary source file in Visual Studio .NET and access its code elements through the code model. (For example, you can't get a FileCodeModel object from a source file in the Solution Items folder.) Only if you assign the source file to a project of the same language does the code model get built for the source file.Table A-2 lists the FileCodeModel methods. We'll cover the Addxxx methods and the Remove method in the section titled "Generating Code"; for a complete treatment of the CodeElementFromPoint method, see the section titled "Getting a CodeElement from a Point Object" in Chapter 12.
Method | Description |
---|---|
AddAttribute | Creates a new top-level attribute |
AddClass | Creates a new top-level class |
AddDelegate | Creates a new top-level delegate |
AddEnum | Creates a new top-level enumeration |
AddFunction | Creates a new top-level function |
AddInterface | Creates a new top-level interface |
AddNamespace | Creates a new top-level namespace |
AddStruct | Creates a new top-level structure |
AddVariable | Creates a new top-level variable |
CodeElementFromPoint | Returns the code element containing the given TextPoint object |
Remove | Removes the specified code element |
Property | Description |
---|---|
CodeElements | Returns a collection of top-level CodeElement objects defined in the associated project |
DTE | Returns the DTE object |
IsCaseSensitive | Returns whether the project's programming language is case-sensitive |
Language | Returns a GUID representing the project's programming language |
Parent | Returns the parent Project object |
Method | Description |
---|---|
AddAttribute | Creates a new top-level attribute |
AddClass | Creates a new top-level class |
AddDelegate | Creates a new top-level delegate |
AddEnum | Creates a new top-level enumeration |
AddFunction | Creates a new top-level function |
AddInterface | Creates a new top-level interface |
AddNamespace | Creates a new top-level namespace |
AddStruct | Creates a new top-level structure |
AddVariable | Creates a new top-level variable |
CodeTypeFromFullName | Returns a CodeType object representing the given code element |
CreateCodeTypeRef | Returns a CodeTypeRef object representing the type of the fully qualified name |
IsValidID | Returns whether a specified name is a valid programmatic identifier for the current language |
Remove | Removes the specified code element from the source file |
Constant | Description |
---|---|
vsCMTypeRefOther | Data type not in this table |
vsCMTypeRefCodeType | CodeType |
vsCMTypeRefArray | Array |
vsCMTypeRefVoid | Void |
vsCMTypeRefPointer | Pointer |
vsCMTypeRefString | String |
vsCMTypeRefObject | Object |
vsCMTypeRefByte | Byte |
vsCMTypeRefChar | Character |
vsCMTypeRefShort | Short integer |
vsCMTypeRefInt | Integer |
vsCMTypeRefLong | Long integer |
vsCMTypeRefFloat | Floating point |
vsCMTypeRefDouble | Double-precision floating point |
vsCMTypeRefDecimal | Decimal |
vsCMTypeRefBool | Boolean |
vsCMTypeRefVariant | Variant |
CodeElement
The CodeElement object serves as a kind of "base class" for the other code model objects by providing a set of properties common to all of the kinds of programming constructs. Table A-6 lists the CodeElement properties.
Property | Description |
---|---|
Children | Returns a collection of all CodeElement objects related to this code element |
Collection | Returns the parent CodeElements collection |
DTE | Returns the DTE object |
EndPoint | Returns a TextPoint object that marks the end of the code element definition |
Extender [*] | Returns the requested extender object |
ExtenderCATID | Returns the extender category ID |
ExtenderNames | Returns the names of the available extender objects |
FullName | Returns the fully qualified name of the code element |
InfoLocation | Returns a vsCMInfoLocation value that describes where the code element is defined |
IsCodeType | Returns whether the code element is a CodeType |
Kind | Returns a vsCMElement value that describes the specific type of the code element |
Language | Returns the programming language used to create the code element |
Name | Sets or returns the name of the code element |
ProjectItem | Returns the ProjectItem that contains the code element |
StartPoint | Returns a TextPoint object that marks the beginning of the code element definition |
[*] C# won't allow you to reference this property using property syntax because its get accessor takes a parameter. Use an explicit call to the get accessor instead.
The three most important CodeElement properties are Name, FullName, and Kind. The Name propertyCodeElement's only read/write propertyallows you to retrieve and change the code element's name programmatically. (Visual Basic doesn't implement the Name property's write functionality.) Note, however, that you can't always change the name of a code elementfor example, you can't change the name of a constructor because a constructor must have the same name as its parent class. The FullName property returns the code element's fully qualified name. The Kind property returns a vsCMElement value that identifies the underlying code construct. The vsCMElement enumeration has 40 constants representing the most common code constructs you'll encounter (as well as some of the more obscure ones that we hope you'll never have to deal with).The Children property returns a CodeElements collection that contains all the code elements related to this one. Languages aren't required to support this propertyand most languages don't support it. Of the four languages that come in the Visual Studio .NET box, only Visual C++ uses the Children property.NoteAs you learn more about the code model, you'll find that Visual C++ contributes to many of its idiosyncrasies. When Visual Studio .NET invited the different language groups to join the code model party, Visual C++ showed up with a code model of its own. For the most part, Visual C++ did its job implementing the Visual Studio .NET code model (by way of its own code model, of course), but some accommodations were made in the Visual Studio .NET code model to allow more access to the Visual C++ native code model. The result is the occasional oddball property, such as CodeElement.Children, and the overrepresentation of Visual C++ in different areas of the code model, such as the constants in the vsCMElement enumeration.The InfoLocation property returns a value from the vsCMInfoLocation enumeration that lets you know where to find the code construct. Table A-7 lists the vsCMInfoLocation constants.
Constant | Description |
---|---|
vsCMInfoLocationProject | Code element lives in a project file. |
vsCMInfoLocationExternal | Code element lives in an external file. |
vsCMInfoLocationVirtual | This constant isn't used in Visual Studio .NET 2003. |
vsCMInfoLocationNone | Code model is unable to determine the location of the code element. |
Method | Description |
---|---|
GetEndPoint | Returns a TextPoint object that marks the end of the code element definition |
GetStartPoint | Returns a TextPoint object that marks the beginning of the code element definition |
Constant | Returns |
---|---|
vsCMPartName | The name of the code construct |
vsCMPartAttributes | The attributes that apply to the code construct, minus the attribute delimiters |
vsCMPartHeader | The header of the code construct |
vsCMPartWhole | The entire code construct |
vsCMPartBody | The body of the code construct, minus the body delimeters |
vsCMPartNavigate | The location in the source code to which the caret moves when you double-click on an element in Class View |
vsCMPartAttributesWithDelimiter | The applicable attributes and the attribute delimiters |
vsCMPartBodyWithDelimiter | The body of the code construct and its delimiters |
vsCMPartHeaderWithAttributes | The code construct's header and its attributes |
vsCMPartWholeWithAttributes | The entire code construct and its attributes |
Specialized Code Model Objects
This section describes the various objects that correspond directly to specific code constructs. Each of these objects aggregates its specific members with the CodeElement members, which makes for pretty long member lists in the Help files. In the following tables, we factored out the common CodeElement members so we could concentrate on the members specific to each type. We also factored out the properties in Table A-10they're not part of the CodeElement properties, but they're common to all the types described in this section.
Property | Description |
---|---|
Comment | Sets or returns the comment associated with the code element |
DocComment | Sets or returns the code element's document comments |
Parent | Returns the parent CodeElements collection |
CodeNamespace
The CodeNamespace object corresponds to a .NET namespace construct (package in J#, namespace in C# and C++, and Namespace in Visual Basic). CodeNamespace includes the properties listed in Table A-11.
Property | Description |
---|---|
CodeElement properties | See Table A-6. |
Other common properties | See Table A-10. |
Members | Returns the top-level code elements contained by the namespace. |
Method | Description |
---|---|
CodeElement methods | See Table A-7. |
AddClass | Creates a new class within the namespace. |
AddDelegate | Creates a new delegate within the namespace. |
AddEnum | Creates a new enumeration within the namespace. |
AddInterface | Creates a new interface within the namespace. |
AddNamespace | Creates a new namespace within the namespace. |
AddStruct | Creates a new structure within the namespace. |
Remove | Removes the specified code element. |
CodeType
The CodeType object is a kind of generic object, like CodeElement, that corresponds roughly to what would be a type in the .NET Framework. The CodeType object allows you to treat certain code model typesCodeClass, CodeStruct, CodeDelegate, CodeInterface, and CodeEnumas if they were the same kind of object. Table A-13 lists the CodeType properties.
Property | Description |
---|---|
CodeElement properties | See Table A-6. |
Other common properties | See Table A-10. |
Access | Sets or returns the access modifiers. |
Attributes | Returns a collection of attributes. |
Bases | Returns a collection of base types. |
DerivedTypes | Returns a collection of derived types. |
IsDerivedFrom [*] | Returns whether this type has another type as a base. |
Members | Returns a collection of top-level code elements contained by this type. |
Namespace | Returns a CodeNamespace object representing the parent namespace. |
[*] C# won't allow you to reference this property using property syntax because its get accessor takes a parameter. Use an explicit call to the get accessor instead.
The Access property sets or returns a vsCMAccess value that determines the code element's access (such as public, private, and so on). Be aware that the CodeType.Access write functionality doesn't work for Visual C++ and Visual Basic. Table A-14 lists the vsCMAccess enumeration constants.
Constant | Description |
---|---|
vsCMAccessPublic | Public access |
vsCMAccessPrivate | Private access |
vsCMAccessProject | Project access |
vsCMAccessProtected | Protected access |
vsCMAccessProjectOrProtected | Combination of project and protected access |
vsCMAccessDefault | Default access |
vsCMAccessAssemblyOrFamily | Assembly or family access |
vsCMAccessWithEvents | WithEvents access |
Method | Description |
---|---|
CodeElement methods | See Table A-7. |
AddAttribute | Creates a new attribute. |
AddBase | Adds a new base type. |
RemoveBase | Removes a base type. |
RemoveMember | Removes a member code element. |
CodeClass and CodeStruct
The CodeClass and CodeStruct objects represent classes and structures, respectively. In the C family of programming languages, classes and structures are intimately relatedbeginning C++ programming books often introduce the class construct as a struct whose members are private by default. The code model also treats the CodeClass and CodeStruct similarly; in fact, the two objects share exactly the same properties and methods, which is why we group them together here, beginning with Table A-16.
Property | Description |
---|---|
CodeElement properties | See Table A-6. |
CodeType properties | See Table A-11. |
Other common properties | See Table A-10. |
ImplementedInterfaces | Returns a collection of implemented interfaces. |
IsAbstract | Sets or returns whether this item is abstract. |
Method | Description |
---|---|
CodeElement methods | See Table A-7. |
CodeType methods | See Table A-12. |
AddClass | Creates a new class within the class or structure. |
AddDelegate | Creates a new delegate within the class or structure. |
AddEnum | Creates a new enumeration within the class or structure. |
AddFunction | Creates a new function within the class or structure. |
AddImplementedInterface | Adds an implemented interface to the class or structure. |
AddProperty | Creates a new property within the class or structure. |
AddStruct | Creates a new structure within the class or structure. |
AddVariable | Creates a new variable within the class or structure. |
RemoveInterface | Removes an implemented interface from the class or structure. |
CodeInterface
The CodeInterface object encapsulates an interface code construct. Table A-18 shows the CodeInterface properties; as you can see from the table, CodeInterface doesn't define any interface-specific properties.
Property | Description |
---|---|
CodeElement properties | See Table A-6. |
CodeType properties | See Table A-11. |
Other common properties | See Table A-10. |
Method | Description |
---|---|
CodeElement methods | See Table A-7. |
CodeType methods | See Table A-12. |
AddFunction | Creates a new function within the interface. |
AddProperty | Creates a new property within the interface. |
CodeEnum
The CodeEnum object represents an enumeration code construct. CodeEnum doesn't define any enumeration-specific properties, as you can see from Table A-20.
Property | Description |
---|---|
CodeElement properties | See Table A-6. |
CodeType properties | See Table A-11. |
Other common properties | See Table A-10. |
Method | Description |
---|---|
CodeElement methods | See Table A-7. |
CodeType methods | See Table A-12. |
AddMember | Creates a new enumeration constant. |
CodeDelegate
The CodeDelegate object represents a delegate code construct. Table A-22 lists the CodeDelegate properties.
Property | Description |
---|---|
CodeElement properties | See Table A-6. |
CodeType properties | See Table A-11. |
Other common properties | See Table A-10. |
BaseClass | Returns a CodeClass object that represents the delegate's base class. |
Parameters | Returns a CodeElements collection containing the delegate's parameters. |
Prototype [*] | Returns the delegate's prototype. |
Type | Sets or returns a CodeTypeRef object representing the delegate's type. |
[*] C# won't allow you to reference this property using property syntax because its get accessor takes a parameter. Use an explicit call to the get accessor instead.
The BaseClass property always returns a CodeClass object that represents System.Delegate. Every language except Visual C++ implements the BaseClass property.The Parameters property returns a collection of CodeElement objects that contains an item for each parameter of the delegate; Visual C++ doesn't implement this property.The Prototype property returns the delegate's prototype as a string. The Prototype property accepts values from the vsCMPrototype enumeration that determine which aspects of the prototype to return. The vsCMPrototype constants listed in Table A-23 are bit flags, so you can combine them to customize the string returned by Prototype. For example, the combination of vsCMPrototypeFullname, vsCMPrototypeType, vsCMPrototypeParamNames, and vsCMPrototypeParamTypes would return all information about a particular delegate. All of the languages except Visual C++ implement this property.
Constant | Description |
---|---|
vsCMPrototypeFullname | Returns the fully qualified name |
vsCMPrototypeNoName | Omits the name |
vsCMPrototypeClassName | Returns the name and class prefix |
vsCMPrototypeParamTypes | Returns the parameter types |
vsCMPrototypeParamNames | Returns the parameter names |
vsCMPrototypeParamDefaultValues | Returns the parameter default values |
vsCMPrototypeUniqueSignature | Returns a unique string based on the prototype |
vsCMPrototypeType | Returns the type |
vsCMPrototypeInitExpression | Returns the initialization expression |
Method | Description |
---|---|
CodeElement methods | See Table A-7. |
CodeType methods | See Table A-12. |
AddParameter | Creates a new delegate parameter. |
RemoveParameter | Removes a delegate parameter. |
CodeVariable
A CodeVariable object represents a variable declaration. Table A-25 lists the CodeVariable properties.
Property | Description |
---|---|
CodeElement properties | See Table A-6. |
Other common properties | See Table A-10. |
Access | Sets or returns the variable's access modifiers. |
Attributes | Returns a collection of the variable's attributes. |
InitExpression | Sets or returns the variable's initialization code. |
IsConstant | Sets or returns whether the variable is constant. |
IsShared | Sets or returns whether the variable is a shared class variable. |
Prototype [*] | Returns the variable's declaration. |
Type | Sets or returns a CodeTypeRef object representing the variable's type. |
[*] C# won't allow you to reference this property using property syntax because its get accessor takes a parameter. Use an explicit call to the get accessor instead.
The Access property is similar to the CodeType.Access property and takes the same vsCMAccess values found in Table A-14. All languages implement the Access property's read functionality; only C# and J# implement the write functionality.The Attributes property returns a CodeElements collection that contains an item of type CodeAttribute for each attribute of the variable.The InitExpression property lets you read or write the variable's initialization expression. All languages support the InitExpression read functionality, and all languages but Visual Basic support the InitExpression write functionality. The expression you pass to the InitExpression property shouldn't contain an initialization operator because the correct operator is supplied by the language. Also, be aware that InitExpression won't validate the expression you specify.The IsConstant property lets you read or write whether the variable is declared as a constant. All languages implement the IsConstant read functionality, but the J# implementation always returns False. Every language except Visual Basic implements the IsConstant write functionality.The IsShared property lets you read or write whether a variable is a class shared variable (True) or a class instance variable (False). The read functionality of IsShared works for all languages and the write functionality works for C# and J#.The Prototype property returns a string that represents the variable's declaration. This property works the same way as the CodeDelegate.Prototype propertyyou pass it a combination of values from the vsCMPrototype enumeration (listed in Table A-23), and it returns those parts of the declaration that you request. Unlike the InitExpression property, the Prototype property returns the initialization operator along with the initialization expression when passed a value of vsCMPrototypeInitExpression. All languages implement this property.The Type property sets or returns a CodeTypeRef object that represents the variable's type. All languages implement the Type property's read functionality, but only Visual C++ implements the write functionality.Table A-26 lists the CodeVariable methods. You can find an explanation of these methods in the section titled "Generating Code."
Method | Description |
---|---|
CodeElement methods | See Table A-7. |
AddAttribute | Creates a new attribute for the variable. |
CodeProperty
The CodeProperty object represents a property code construct. Table A-27 lists the CodeProperty properties. Note that the J# implementation doesn't recognize property declarationsinstead, J# interprets the property's get_xxx and set_xxx methods as CodeFunction objects. Consequently, the explanations that follow Table A-27 won't include mention of J#.
Property | Description |
---|---|
CodeElement properties | See Table A-6. |
Other common properties | See Table A-10. |
Access | Sets or returns the property's access modifiers. |
Attributes | Returns a collection of the property's attributes. |
Getter | Sets or returns a CodeFunction object representing the property's getter function. |
Prototype | Returns the property's prototype. |
Setter | Sets or returns a CodeFunction object representing the property's setter function. |
Type | Sets or returns a CodeTypeRef representing the property's type. |
Method | Description |
---|---|
CodeElement methods | See Table A-7. |
AddAttribute | Creates a new attribute for the property. |
CodeAttribute
The CodeAttribute object corresponds to an attribute as defined by the .NET Framework. Table A-29 shows the CodeAttribute properties.
Property | Description |
---|---|
CodeElement properties | See Table A-6. |
Other common properties | See Table A-10. |
Value | Sets or returns the attribute's value. |
Method | Description |
---|---|
CodeElement methods | See Table A-7. |
Delete | Removes the attribute. |
CodeFunction
The CodeFunction object represents a function or sub procedure code construct. Table A-31 lists the CodeFunction properties.
Property | Description |
---|---|
CodeElement properties | See Table A-6. |
Other common properties | See Table A-10. |
Access | Sets or returns the function's access modifiers. |
Attributes | Returns a collection of the function's attributes. |
CanOverride | Sets or returns whether the function can be overridden. |
FunctionKind | Returns a vsCMFunction enumeration value that describes what kind of function the function is. |
IsOverloaded | Returns whether this function is overloaded. |
IsShared | Sets or returns whether the function is statically defined. |
MustImplement | Sets or returns whether the function is abstract. |
Overloads | Returns a CodeElements collection containing the function's overloads. |
Parameters | Returns a CodeElements collection containing the function's parameters. |
Prototype [*] | Returns the function's declaration. |
Type | Sets or returns a CodeTypeRef representing the function's type. |
[*] C# won't allow you to reference this property using property syntax because its get accessor takes a parameter. Use an explicit call to the get accessor instead.
The Access property lets you read or write the function's access modifiers. (See Table A-14.) All languages implement the read functionality, and all languages but Visual Basic implement the write functionality.The Attributes property returns a CodeElements collection containing an item of type CodeAttribute for each attribute that applies to the function.The CanOverride property lets you read or write whether the function is overridable. All the languages implement the read functionality, but Visual C++ always returns True. C# and J# implement the write functionality; however, J# will only let you change from a nonoverridable (final) function to an overridable function.The FunctionKind property returns one of the vsCMFunction values from Table A-32. Every language implements this property.
Constant | Description |
---|---|
vsCMFunctionOther | A kind of function not listed in this table |
vsCMFunctionConstructor | A constructor |
vsCMFunctionPropertyGet | A property getter |
vsCMFunctionPropertyLet | A property letter |
vsCMFunctionPropertySet | A property setter |
vsCMFunctionPutRef | A put reference |
vsCMFunctionPropertyAssign | A property assign |
vsCMFunctionSub | A Sub procedure |
vsCMFunctionFunction | A function |
vsCMFunctionTopLevel | A top-level function |
vsCMFunctionDestructor | A destructor |
vsCMFunctionOperator | An operator |
vsCMFunctionVirtual | A virtual function |
vsCMFunctionPure | A pure virtual function |
vsCMFunctionConstant | A constant |
vsCMFunctionShared | A shared function |
vsCMFunctionInline | An inline function |
Method | Description |
---|---|
CodeElement methods | See Table A-7. |
AddAttribute | Creates a new attribute. |
AddParameter | Creates a new parameter. |
RemoveParameter | Removes a parameter. |
CodeParameter
The CodeParameter object represents a parameter of a function, sub procedure, or delegate. Table A-34 lists the CodeParameter properties.
Property | Description |
---|---|
CodeElement properties | See Table A-6. |
Other common properties | See Table A-10. |
Attributes | Returns a collection of the parameter's attributes. |
Type | Sets or returns a CodeTypeRef representing the parameter's type. |
Method | Description |
---|---|
CodeElement methods | See Table A-7. |
AddAttribute | Creates a new attribute. |
CodeTypeRef
The CodeTypeRef object represents the type of a function, delegate, property, variable, or parameter. Table A-36 lists the CodeTypeRef properties.
Property | Description |
---|---|
AsFullName | Returns the type's fully qualified name |
AsString | Returns the language's keyword for the type |
CodeType | Sets or returns the CodeType associated with the type |
DTE | Returns the DTE object |
ElementType | Sets or returns the type of the array's elements |
Parent | Returns the parent CodeElement |
Rank | Sets or returns the number of dimensions of the array |
TypeKind | Returns the base type |