CodeElement2.Kind 属性
获取一个枚举,该枚举定义代码元素的类型。
命名空间: EnvDTE80
程序集: EnvDTE80(在 EnvDTE80.dll 中)
语法
声明
ReadOnly Property Kind As vsCMElement
vsCMElement Kind { get; }
property vsCMElement Kind {
vsCMElement get ();
}
abstract Kind : vsCMElement with get
function get Kind () : vsCMElement
属性值
类型:vsCMElement
一个 vsCMElement2 值,该值指定代码元素类型。
备注
备注
在进行某些类型的编辑之后,代码模型元素(如类、结构、函数、特性、委托等)的值可能是非确定性的,这意味着不能指望它们的值总是保持不变。有关更多信息,请参见 使用代码模型查找代码 (Visual Basic) 中的“代码模型元素的值可能会更改”一节。
示例
[Visual Basic]
Sub KindExample(ByVal dte As DTE2)
' NOTE: This example requires a reference to the System.Text
' namespace.
' Before running this example, open a code document from a project.
Dim item As ProjectItem = dte.ActiveDocument.ProjectItem
Dim sb As New StringBuilder
RecurseElements(item.FileCodeModel.CodeElements, 0, sb)
MsgBox(item.Name & " contains the following elements:" & vbCrLf & _
vbCrLf & sb.ToString())
End Sub
Sub RecurseElements(ByVal elems As CodeElements, _
ByVal level As Integer, ByVal sb As StringBuilder)
Dim elem As CodeElement
For Each elem In elems
' Add element to the list of names.
sb.Append(" "c, level * 8)
sb.Append(elem.Name & " [" & elem.Kind.ToString() & "]" & _
vbCrLf)
' Call this function recursively if element has children.
If elem.Kind = vsCMElement2.vsCMElementNamespace Then
RecurseElements(CType(elem, CodeNamespace).Members, _
level + 1, sb)
ElseIf elem.IsCodeType Then
RecurseElements(CType(elem, CodeType).Members, _
level + 1, sb)
End If
Next
End Sub
[C#]
public void KindExample(DTE2 dte)
{
// NOTE: This example requires a reference to the
// System.Text namespace.
// Before running this example, open a code document from
// a project.
ProjectItem item = dte.ActiveDocument.ProjectItem;
StringBuilder sb = new StringBuilder();
RecurseElements(item.FileCodeModel.CodeElements, 0, sb);
MessageBox.Show(item.Name +
" contains the following elements:\n\n" + sb.ToString());
}
void RecurseElements(CodeElements elems, int level, StringBuilder sb)
{
foreach (CodeElement elem in elems)
{
// Add element to the list of names.
sb.Append(' ', level * 8);
sb.Append(elem.Name + " [" + elem.Kind.ToString() + "]\n");
// Call this function recursively if element has children.
if (elem.Kind == vsCMElement.vsCMElementNamespace)
RecurseElements(((CodeNamespace)elem).Members,
level + 1, sb);
else if (elem.IsCodeType)
RecurseElements(((CodeType)elem).Members, level + 1, sb);
}
}
.NET Framework 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关详细信息,请参阅通过部分受信任的代码使用库。