共用方式為


FileCodeModel.AddStruct 方法

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

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

語法

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

參數

  • Position
    型別:System.Object
    選擇項。預設値 = 0。其後要加入新項目的程式碼項目。如果這個值是 CodeElement,則會緊接在其後加入新項目。
    如果這個值是長資料型別,那麼 AddStruct 會指示其後要加入新項目的項目。
    由於集合是從 1 開始計算,傳遞 0 表示新的項目必須放在集合起始的位置。值為 -1 時表示這個項目必須放在集合結尾的位置。
  • Bases
    型別:System.Object
    必要項。預設值為 Nothing。Variant,可存放具有完整型別名稱或 CodeInterface 物件 (新介面會從其衍生) 的 SafeArray。
  • ImplementedInterfaces
    型別:System.Object
    必要項。預設值為 Nothing。完整型別名稱或 CodeInterface 物件的 SafeArray,分別表示新類別承諾要實作的介面。

傳回值

型別:EnvDTE.CodeStruct
CodeStruct 物件。

備註

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

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

範例

Sub AddStructExample2(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.
        projItem.FileCodeModel.AddStruct("TestStruct", , 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 AddStructExample2(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.
        projItem.FileCodeModel.AddStruct("TestStruct", -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 安全性

請參閱

參考

FileCodeModel 介面

EnvDTE 命名空間

其他資源

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