FileCodeModel.AddClass 方法
建立新的類別程式碼建構,並在正確位置插入程式碼。
命名空間: EnvDTE
組件: EnvDTE (在 EnvDTE.dll 中)
語法
'宣告
Function AddClass ( _
Name As String, _
Position As Object, _
Bases As Object, _
ImplementedInterfaces As Object, _
Access As vsCMAccess _
) As CodeClass
CodeClass AddClass(
string Name,
Object Position,
Object Bases,
Object ImplementedInterfaces,
vsCMAccess Access
)
CodeClass^ AddClass(
String^ Name,
Object^ Position,
Object^ Bases,
Object^ ImplementedInterfaces,
vsCMAccess Access
)
abstract AddClass :
Name:string *
Position:Object *
Bases:Object *
ImplementedInterfaces:Object *
Access:vsCMAccess -> CodeClass
function AddClass(
Name : String,
Position : Object,
Bases : Object,
ImplementedInterfaces : Object,
Access : vsCMAccess
) : CodeClass
參數
- Name
型別:System.String
必要項。類別的名稱。
- Position
型別:System.Object
選擇項。預設値 = 0。其後要加入新項目的程式碼項目。如果這個值是 CodeElement,則會緊接在其後加入新項目。
如果這個值是長資料型別,那麼 AddClass 會指示其後要加入新項目的項目。
由於集合是從 1 開始計算,傳遞 0 表示新的項目必須放在集合起始的位置。值為 -1 時表示這個項目必須放在集合結尾的位置。
- Bases
型別:System.Object
選擇項。預設値 = 0。完整型別名稱或從新類別繼承其實作之 CodeClass 物件的 SafeArray。
- ImplementedInterfaces
型別:System.Object
選擇項。預設値 = 0。完整型別名稱或 CodeInterface 物件的 SafeArray,分別表示新類別承諾要實作的介面。
- Access
型別:EnvDTE.vsCMAccess
選擇項。vsCMAccess 常數。
傳回值
型別:EnvDTE.CodeClass
CodeClass 物件。
備註
引數的正確性取決於程式碼模型後的程式語言。
範例
Sub AddClassExample2(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 class.
projItem.FileCodeModel.AddClass("TestClass", , 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 AddClassExample2(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 class.
projItem.FileCodeModel.AddClass("TestClass", -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 安全性
- 完全信任立即呼叫者。這個成員無法供部分信任的程式碼使用。如需詳細資訊,請參閱從部分受信任程式碼使用程式庫。