FileCodeModel2.AddStruct 方法 (String, Object, Object, Object, vsCMAccess)
建立新的結構程式碼建構並將程式碼插入正確位置。
命名空間: 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,則會緊接在其後加入新項目。
如果這個值是長整數資料型別,那麼 AddStruct 會指示其後要加入新項目的項目。
由於集合是從 1 開始計算,傳遞 0 表示新的項目必須放在集合起始的位置。值為 -1 時表示這個項目必須放在集合結尾的位置。
- Bases
型別:System.Object
必要項。預設值為 Nothing。Variant,可存放具有完整型別名稱或 CodeInterface 物件 (新介面會從其衍生) 的 SafeArray。
- 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 安全性
- 完全信任立即呼叫者。這個成員無法供部分信任的程式碼使用。如需詳細資訊,請參閱從部分受信任程式碼使用程式庫。