CodeModel2.AddStruct 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建新的结构代码构造,并将代码插入正确的位置。
EnvDTE::CodeStruct AddStruct(std::wstring const & Name, winrt::Windows::Foundation::IInspectable const & Location, 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(11)]
public EnvDTE.CodeStruct AddStruct (string Name, object Location, object Position, object Bases, object ImplementedInterfaces, EnvDTE.vsCMAccess Access = EnvDTE.vsCMAccess.vsCMAccessDefault);
[<System.Runtime.InteropServices.DispId(11)>]
abstract member AddStruct : string * obj * obj * obj * obj * EnvDTE.vsCMAccess -> EnvDTE.CodeStruct
Public Function AddStruct (Name As String, Location As Object, Optional Position As Object, Optional Bases As Object, Optional ImplementedInterfaces As Object, Optional Access As vsCMAccess = EnvDTE.vsCMAccess.vsCMAccessDefault) As CodeStruct
参数
- Name
- String
必需。 新结构的名称。
- Location
- Object
必需。 用于新结构定义的路径和文件名。 文件名对项目文件是相对的还是绝对的取决于所使用的语言。 如果文件还不是项目项,则将被添加到项目中。 如果无法创建文件并将其添加到项目,则将 AddStruct(String, Object, Object, Object, Object, vsCMAccess) 失败。
- Position
- Object
可选。 默认值 = 0。 将在其后添加新元素的代码元素。 如果该值为 CodeElement,则紧跟在其后添加新元素。
如果该值为 Long 数据类型,则 AddStruct(String, Object, Object, Object, Object, vsCMAccess) 指示要在哪个元素后添加新元素。
因为集合从 1 开始计数,所以传递 0 指示应将新元素放置在集合的开始处。 值为 -1 表示应将元素放在结尾处。
- Bases
- Object
必需。 默认值为 Nothing。 包含完全限定类型名称的 SafeArray 或 CodeInterface 新接口派生自的对象的变量。
- ImplementedInterfaces
- Object
必需。 默认值为 Nothing。 完全限定类型名称的 SafeArray,或 CodeInterfaces 表示新类承诺实现的接口。
- Access
- vsCMAccess
可选。 一个 vsCMAccess 常数。
返回
CodeStruct 对象。
实现
- 属性
示例
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;
}
注解
Native Visual C++ 要求其完全限定的类型名称以冒号分隔 (:: ) 格式。 所有其他语言支持句号分隔格式。
参数的正确性由代码模型后面的语言确定。
备注
在进行了某些类型的编辑之后,代码模型元素(例如类、结构、函数、特性、委托等)的值可能变为非确定性的,这意味着不能确定它们的值始终保持不变