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

定义

在类中创建一个新结构。

EnvDTE::CodeStruct AddStruct(std::wstring const & Name, winrt::Windows::Foundation::IInspectable const & Position, winrt::Windows::Foundation::IInspectable const & Bases, winrt::Windows::Foundation::IInspectable const & ImplementedInterfaces, EnvDTE::vsCMAccess Access = EnvDTE.vsCMAccess.vsCMAccessDefault);
[System.Runtime.InteropServices.DispId(68)]
public EnvDTE.CodeStruct AddStruct (string Name, object Position, object Bases, object ImplementedInterfaces, EnvDTE.vsCMAccess Access = EnvDTE.vsCMAccess.vsCMAccessDefault);
[<System.Runtime.InteropServices.DispId(68)>]
abstract member AddStruct : string * obj * obj * obj * EnvDTE.vsCMAccess -> EnvDTE.CodeStruct
Public Function AddStruct (Name As String, Optional Position As Object, Optional Bases As Object, Optional ImplementedInterfaces As Object, Optional Access As vsCMAccess = EnvDTE.vsCMAccess.vsCMAccessDefault) As CodeStruct

参数

Name
String

必需。 新结构的名称。

Position
Object

可选。 默认值 = 0。 将在其后添加新元素的代码元素。

如果值为 Long 数据类型,则 Position 方法指示在其后添加新元素的元素。

因为集合从 1 开始计数,所以传递 0 指示应将新元素放置在集合的开始处。 值为 -1 表示应将元素放在结尾处。

Bases
Object

必需。 默认值为 Nothing。 包含完全限定类型名称的 SafeArray 的变量,或 CodeInterface 新结构的派生对象。

ImplementedInterfaces
Object

必需。 默认值为 Nothing。 完全限定类型名或 CodeInterface 对象(表示新类承诺实现的接口)的 SafeArray。

Access
vsCMAccess

可选。 一个 vsCMAccess 常数。

返回

CodeStruct

CodeStruct 对象。

实现

属性

示例

[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  

[C#]

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;  
}  

注解

本机 Visual C++ 要求其完全限定类型名采用冒号分隔 (::) 格式。 所有其他语言支持句号分隔格式。

参数的正确性由代码模型后面的语言确定。

备注

在进行了某些类型的编辑之后,代码模型元素(例如类、结构、函数、特性、委托等)的值可能变为非确定性的,这意味着不能确定它们的值始终保持不变。 有关详细信息,请参阅 "代码模型元素值在 使用代码模型查找代码时 可能发生变化" (Visual Basic) "。

适用于