Share via


CodeClass2.AddAttribute 方法 (String, String, Object)

建立新的屬性程式碼建構並將類別插入正確位置。

命名空間:  EnvDTE80
組件:  EnvDTE80 (在 EnvDTE80.dll 中)

語法

'宣告
Function AddAttribute ( _
    Name As String, _
    Value As String, _
    Position As Object _
) As CodeAttribute
CodeAttribute AddAttribute(
    string Name,
    string Value,
    Object Position
)
CodeAttribute^ AddAttribute(
    String^ Name, 
    String^ Value, 
    Object^ Position
)
abstract AddAttribute : 
        Name:string * 
        Value:string * 
        Position:Object -> CodeAttribute 
function AddAttribute(
    Name : String, 
    Value : String, 
    Position : Object
) : CodeAttribute

參數

  • Name
    型別:System.String
    必要項。新屬性 (Attribute) 的名稱。
  • Value
    型別:System.String
    必要項。屬性的值,對於參數型屬性,它可能是以逗號分隔的參數清單。
  • Position
    型別:System.Object
    選擇項。預設 = 0。其後要加入新項目的程式碼項目。如果這個值是 CodeElement,則會緊接在其後加入新項目。
    如果這個值是長資料型別,那麼 Position 會指示其後要加入新項目的項目。
    由於集合是從 1 開始計算,傳遞 0 表示新的項目必須放在集合起始的位置。值為 -1 時表示這個項目必須放在集合結尾的位置。

傳回值

型別:EnvDTE.CodeAttribute
CodeAttribute 物件。

實作

CodeClass.AddAttribute(String, String, Object)

備註

如果屬性已經存在,AddAttribute 方法會加入另一個屬性。 這對於該屬性或程式碼模型後的語言可能會不正確。

注意事項注意事項

在特定類型的編輯之後,程式碼模型項目 (例如類別、結構、函式、屬性、委派等) 的值可能不具決定性,表示其值不一定維持相同。 如需詳細資訊,請參閱使用程式碼模型探索程式碼 (Visual Basic) 的<程式碼模型項目值可以變更>一節。

範例

[Visual Basic]

Sub AddAttributeExample(ByVal dte As DTE2)
    ' Before running this example, open a code document from a project
    ' and place the insertion point inside a class definition.
    Try
        ' Retrieve the CodeClass at the insertion point.
        Dim sel As TextSelection = _
            CType(dte.ActiveDocument.Selection, TextSelection)
        Dim cls As CodeClass = _
            CType(sel.ActivePoint.CodeElement( _
            vsCMElement.vsCMElementClass), CodeClass)

        ' Add an attribute to the class.
        cls.AddAttribute("TestAttribute", "TestValue")
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

[C#]

 public void AddAttributeExample(DTE2 dte)
{
    // Before running this example, open a code document from a project
    // and place the insertion point inside a class definition.
    try
    {
        // Retrieve the CodeClass at the insertion point.
        TextSelection sel = 
            (TextSelection)dte.ActiveDocument.Selection;
        CodeClass cls = 
            (CodeClass)sel.ActivePoint.get_CodeElement(
             vsCMElement.vsCMElementClass);

        // Add an attribute to the class.
        cls.AddAttribute("TestAttribute", "TestValue", -1);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

.NET Framework 安全性

請參閱

參考

CodeClass2 介面

AddAttribute 多載

EnvDTE80 命名空間

其他資源

HOW TO:編譯和執行 Automation 物件模型程式碼範例

使用程式碼模型探索程式碼 (Visual Basic)

使用程式碼模型探索程式碼 (Visual C#)