In This Article

Context

The C# language can return detailed context information about any offset in a document. The context includes data such as containing AST node, target expression, and more. This sort of information is essential in passing to the resolver to determine what to display in automated IntelliPrompt.

Obtaining Context Information

Context information can be retrieved via the use of the CSharpContextFactory class. Its CreateContext method accepts a TextSnapshotOffset and returns the IDotNetContext for that offset.

This code retrieves an IDotNetContext for the caret's offset (the end selection offset in the active view):

TextSnapshotOffset snapshotOffset = syntaxEditor.ActiveView.Selection.EndSnapshotOffset;
IDotNetContext context = new CSharpContextFactory().CreateContext(snapshotOffset);

A context kind (via the DotNetContextKind enumeration) can also be passed to a CreateContext method overload. The default value is Self, but you can also pass SelfAndSiblings to indicate that alternate logic should be used when building the context. When SelfAndSiblings is passed, the context factory will return a context object that focuses on the "parent" of the offset. This allows resolver operations that use the context to enumerate a list of accessible members.

.NET Context

IDotNetContext objects provide .NET language-related context information for a specific snapshot offset, such as the containing AST node, target expression, and more.

These IDotNetContext members are important:

Member Description
ContainingAstNode Property Gets the IAstNode that contains the TextSnapshotOffset.
ContainingAstTypeDeclaration Property Gets the AST TypeDeclaration that contains the TextSnapshotOffset, if any.
InitializationSnapshotRange Property Gets the TextSnapshotRange with which the context was initialized.
ProjectAssembly Property Gets the IProjectAssembly with which this context is associated.
SnapshotOffset Property Gets the TextSnapshotOffset for which this context was created.
TargetExpression Property Gets the target AST Expression for the context, if known.