创建新的结构代码构造,并将代码插入正确的位置。
命名空间:  EnvDTE80
程序集:  EnvDTE80(在 EnvDTE80.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
参数
- Name
 类型:System.String
 必选。新结构的名称。
- Position
 类型:System.Object
 可选。默认值 = 0。将在其后添加新元素的代码元素。如果该值为 CodeElement,则紧跟在其后添加新元素。
 如果该值为 Long 数据类型,则 AddStruct 指示在哪个元素后添加新元素。
 因为集合从 1 开始计数,所以传递 0 指示应将新元素放置在集合的开始处。值为 -1 表示应将元素放在结尾处。
- Bases
 类型:System.Object
 必选。默认值为 Nothing。一个保存 SafeArray 的变量,该 SafeArray 包含完全限定类型名或新接口派生自的 CodeInterface 对象。
- ImplementedInterfaces
 类型:System.Object
 必选。默认值为 Nothing。一个包含完全限定类型名或 CodeInterface 对象(每个对象都表示新类承诺实现的接口)的 SafeArray。
- Access
 类型:EnvDTE.vsCMAccess
 可选。一个 vsCMAccess 常数。
返回值
类型:EnvDTE.CodeStruct
一个 CodeStruct 对象。
实现
FileCodeModel.AddStruct(String, Object, Object, Object, vsCMAccess)
备注
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 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。