共用方式為


CodeModel2.AddStruct 方法 (String, Object, Object, Object, Object, vsCMAccess)

建立新的結構程式碼建構並將程式碼插入正確位置。

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

語法

'宣告
Function AddStruct ( _
    Name As String, _
    Location As Object, _
    Position As Object, _
    Bases As Object, _
    ImplementedInterfaces As Object, _
    Access As vsCMAccess _
) As CodeStruct
CodeStruct AddStruct(
    string Name,
    Object Location,
    Object Position,
    Object Bases,
    Object ImplementedInterfaces,
    vsCMAccess Access
)
CodeStruct^ AddStruct(
    String^ Name, 
    Object^ Location, 
    Object^ Position, 
    Object^ Bases, 
    Object^ ImplementedInterfaces, 
    vsCMAccess Access
)
abstract AddStruct : 
        Name:string * 
        Location:Object * 
        Position:Object * 
        Bases:Object * 
        ImplementedInterfaces:Object * 
        Access:vsCMAccess -> CodeStruct 
function AddStruct(
    Name : String, 
    Location : Object, 
    Position : Object, 
    Bases : Object, 
    ImplementedInterfaces : Object, 
    Access : vsCMAccess
) : CodeStruct

參數

  • Location
    型別:System.Object
    必要項。新結構定義的路徑和檔名。根據程式語言的不同,檔名可能與專案檔相關或完全一致。如果這個檔案目前不是專案項目,將會將它加入到專案中。如果無法建立這個檔案並加入至專案,則 AddStruct 將會失敗。
  • Position
    型別:System.Object
    選擇項。預設 = 0。其後要加入新項目的程式碼項目。如果這個值是 CodeElement,則會緊接在其後加入新項目。
    如果這個值是長資料型別,那麼 AddStruct 會指示其後要加入新項目的項目。
    由於集合是從 1 開始計算,傳遞 0 表示新的項目必須放在集合起始的位置。值為 -1 時表示這個項目必須放在集合結尾的位置。
  • Bases
    型別:System.Object
    必要項。預設值為 Nothing。Variant,可存放具有完整型別名稱或 CodeInterface 物件 (新介面會從其衍生) 的 SafeArray。
  • ImplementedInterfaces
    型別:System.Object
    必要項。預設值為 Nothing。完整型別名稱或 CodeInterfaces 的 SafeArray,代表新類別承諾要實作的介面。

傳回值

型別:EnvDTE.CodeStruct
CodeStruct 物件。

實作

CodeModel.AddStruct(String, Object, Object, Object, Object, vsCMAccess)

備註

原生 Visual C++ 的完整型別名稱必須使用冒號 (::) 分隔。 其他所有語言都支援句號分隔的格式。

引數的正確性取決於程式碼模型後的程式語言。

注意事項注意事項

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

範例

Sub AddStructExample(ByVal dte As DTE2)

    ' Before running this example, open a code document from a project.
    Try
        Dim projItem As ProjectItem = dte.ActiveDocument.ProjectItem
        Dim cm As CodeModel = projItem.ContainingProject.CodeModel

        ' Initialize the base classes array and the implemented 
        ' interfaces array.
        Dim bases() As Object = {ConvertFullName(cm, "System.Object")}
        Dim interfaces() As Object = { _
            ConvertFullName(cm, "System.IDisposable"), _
            ConvertFullName(cm, "System.IComparable") _
        }

        ' Create a new struct.
        cm.AddStruct("TestStruct", projItem.Name, , bases, interfaces)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

Function ConvertFullName(ByVal cm As CodeModel, _
    ByVal fullName As String) As String

    ' Convert a .NET type name into a C++ type name.
    If (cm.Language = CodeModelLanguageConstants.vsCMLanguageVC) Or _
        (cm.Language = CodeModelLanguageConstants.vsCMLanguageMC) Then
        Return fullName.Replace(".", "::")
    Else
        Return fullName
    End If

End Function
public void AddStructExample(DTE2 dte)
{
    // Before running this example, open a code document from 
    // a project.
    try
    {
        ProjectItem projItem = dte.ActiveDocument.ProjectItem;
        CodeModel cm = projItem.ContainingProject.CodeModel;

        // Initialize the base classes array and the implemented 
        // interfaces array.
        object[] bases = {ConvertFullName(cm, "System.Object")};
        object[] interfaces = {
        ConvertFullName(cm, "System.IDisposable"),
        ConvertFullName(cm, "System.IComparable")
        };

        // Create a new struct.
        cm.AddStruct("TestStruct", projItem.Name, -1, bases, 
            interfaces, vsCMAccess.vsCMAccessPublic);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

string ConvertFullName(CodeModel cm, string fullName)
{
    // Convert a .NET type name into a C++ type name.
    if ((cm.Language == CodeModelLanguageConstants.vsCMLanguageVC) || 
        (cm.Language == CodeModelLanguageConstants.vsCMLanguageMC))
        return fullName.Replace(".", "::");
    else
        return fullName;
}

.NET Framework 安全性

請參閱

參考

CodeModel2 介面

AddStruct 多載

EnvDTE80 命名空間

其他資源

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

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

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