更新:2007 年 11 月
返回源文件中指定位置处的代码元素。
命名空间: EnvDTE80
程序集: EnvDTE80(在 EnvDTE80.dll 中)
语法
声明
Function CodeElementFromPoint ( _
Point As TextPoint, _
Scope As vsCMElement _
) As CodeElement
用法
Dim instance As FileCodeModel2
Dim Point As TextPoint
Dim Scope As vsCMElement
Dim returnValue As CodeElement
returnValue = instance.CodeElementFromPoint(Point, _
Scope)
CodeElement CodeElementFromPoint(
TextPoint Point,
vsCMElement Scope
)
CodeElement^ CodeElementFromPoint(
TextPoint^ Point,
vsCMElement Scope
)
function CodeElementFromPoint(
Point : TextPoint,
Scope : vsCMElement
) : CodeElement
参数
Point
类型:EnvDTE.TextPoint必选。TextPoint 对象,该对象表示需要其代码元素的编辑器位置。
Scope
类型:EnvDTE.vsCMElement必选。vsCMElement 值,该值表示包含编辑器位置的指定类型的代码元素。
返回值
一个 CodeElement 对象。
实现
FileCodeModel.CodeElementFromPoint(TextPoint, vsCMElement)
备注
根据指定的包容范围或粒度,CodeElementFromPoint 返回与 TextPoint 关联的代码元素。如果指定类型的代码元素不包含编辑器位置,则 CodeElementFromPoint 失败。
示例
Sub CodeElementFromPointExample(ByVal dte As DTE2)
' Before running this example, open a code document from a project
' and place the insertion point anywhere inside the source code.
Try
Dim sel As TextSelection = _
CType(dte.ActiveDocument.Selection, TextSelection)
Dim pnt As TextPoint = CType(sel.ActivePoint, TextPoint)
' Discover every code element containing the insertion point.
Dim fcm As FileCodeModel = _
dte.ActiveDocument.ProjectItem.FileCodeModel
Dim elems As String
Dim elem As CodeElement
Dim scope As vsCMElement
For Each scope In [Enum].GetValues(scope.GetType())
elem = fcm.CodeElementFromPoint(pnt, scope)
If IsNothing(elem) = False Then
elems &= elem.Name & " (" & scope.ToString() & ")" & _
vbCrLf
End If
Next
MsgBox("The following elements contain the insertion point:" _
& vbCrLf & vbCrLf & elems)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
public void CodeElementFromPointExample(DTE2 dte)
{
// Before running this example, open a code document from a project
// and place the insertion point anywhere inside the source code.
try
{
TextSelection sel =
(TextSelection)dte.ActiveDocument.Selection;
TextPoint pnt = (TextPoint)sel.ActivePoint;
// Discover every code element containing the insertion point.
FileCodeModel fcm =
dte.ActiveDocument.ProjectItem.FileCodeModel;
string elems = "";
vsCMElement scopes = 0;
foreach (vsCMElement scope in Enum.GetValues(scopes.GetType()))
{
CodeElement elem = fcm.CodeElementFromPoint(pnt, scope);
if (elem != null)
elems += elem.Name + " (" + scope.ToString() + ")\n";
}
MessageBox.Show(
"The following elements contain the insertion point:\n\n" +
elems);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
权限
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。