FileCodeModel2.CodeElementFromPoint 方法 (TextPoint, vsCMElement)
傳回原始程式檔中特定位置上的程式碼項目。
命名空間: EnvDTE80
組件: EnvDTE80 (在 EnvDTE80.dll 中)
語法
'宣告
Function CodeElementFromPoint ( _
Point As TextPoint, _
Scope As vsCMElement _
) As CodeElement
CodeElement CodeElementFromPoint(
TextPoint Point,
vsCMElement Scope
)
CodeElement^ CodeElementFromPoint(
TextPoint^ Point,
vsCMElement Scope
)
abstract CodeElementFromPoint :
Point:TextPoint *
Scope:vsCMElement -> CodeElement
function CodeElementFromPoint(
Point : TextPoint,
Scope : vsCMElement
) : CodeElement
參數
- Point
型別:EnvDTE.TextPoint
必要項。TextPoint 物件,表示您要放置程式碼項目的編輯器位置。
- Scope
型別:EnvDTE.vsCMElement
必要項。vsCMElement 值,表示指定之型別的程式碼項目,項目中含有編輯器位置。
傳回值
型別:EnvDTE.CodeElement
CodeElement 物件。
備註
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.
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())
Try
elem = fcm.CodeElementFromPoint(pnt, scope)
elems &= elem.Name & " (" & scope.ToString() & ")" & vbCrLf
Catch ex As Exception
‘don’t do anything -
‘this is expected when no code elements are in scope
End Try
Next
MsgBox("The following elements contain the insertion point:" _
& vbCrLf & vbCrLf & elems)
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.
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()))
{
try
{
CodeElement elem = fcm.CodeElementFromPoint(pnt, scope);
elems += elem.Name + " (" + scope.ToString() + ")\n";
}
catch (COMException ex)
{
//don’t do anything – this is expected if there is
//no code element in the scope
}
}
MessageBox.Show(
"The following elements contain the insertion point:\n\n" +
elems);
}
.NET Framework 安全性
- 完全信任立即呼叫者。這個成員無法供部分信任的程式碼使用。如需詳細資訊,請參閱從部分受信任程式碼使用程式庫。