CodeModel2.AddInterface 方法 (String, Object, Object, Object, vsCMAccess)

更新:2007 年 11 月

创建新的接口代码构造,并将代码插入正确的位置。

命名空间:  EnvDTE80
程序集:  EnvDTE80(在 EnvDTE80.dll 中)

语法

声明
Function AddInterface ( _
    Name As String, _
    Location As Object, _
    Position As Object, _
    Bases As Object, _
    Access As vsCMAccess _
) As CodeInterface
用法
Dim instance As CodeModel2
Dim Name As String
Dim Location As Object
Dim Position As Object
Dim Bases As Object
Dim Access As vsCMAccess
Dim returnValue As CodeInterface

returnValue = instance.AddInterface(Name, _
    Location, Position, Bases, Access)
CodeInterface AddInterface(
    string Name,
    Object Location,
    Object Position,
    Object Bases,
    vsCMAccess Access
)
CodeInterface^ AddInterface(
    String^ Name, 
    Object^ Location, 
    Object^ Position, 
    Object^ Bases, 
    vsCMAccess Access
)
function AddInterface(
    Name : String, 
    Location : Object, 
    Position : Object, 
    Bases : Object, 
    Access : vsCMAccess
) : CodeInterface

参数

  • Name
    类型:System.String

    必选。要添加的接口的名称。

  • Location
    类型:System.Object

    必选。新接口定义的路径和文件名。根据语言的不同,文件名是项目文件的相对名称或绝对名称。如果该文件还不是项目项,则将该文件添加到项目中。如果无法创建该文件并将其添加到项目中,则 AddInterface 失败。

  • Position
    类型:System.Object

    可选。默认为 0。将在其后添加新元素的代码元素。如果该值为 CodeElement,则紧跟在其后添加新元素。

    如果该值为 Long 数据类型,则 AddInterface 指示在哪个元素后添加新元素。

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

  • Bases
    类型:System.Object

    可选。默认值为 Nothing 或 nullnull 引用(在 Visual Basic 中为 Nothing)。一个保存 SafeArray 的变量,该 SafeArray 包含完全限定类型名或新接口派生自的 CodeInterface 对象。

返回值

类型:EnvDTE.CodeInterface

一个 CodeInterface 对象。

实现

CodeModel.AddInterface(String, Object, Object, Object, vsCMAccess)

备注

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

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

说明:

在进行某些类型的编辑之后,代码模型元素(如类、结构、函数、属性、委托等)的值可能是非确定性的,这意味着不能指望它们的值总是保持不变。有关更多信息,请参见 使用代码模型查找代码 (Visual Basic) 中的“代码模型元素的值可能会更改”一节。

示例

Sub AddInterfaceExample(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 interfaces array.
        Dim bases() As Object = { _
            ConvertFullName(cm, "System.IDisposable"), _
            ConvertFullName(cm, "System.IComparable") _
        }

        ' Create a new class.
        cm.AddInterface("TestInterface", projItem.Name, , bases)
    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 AddInterfaceExample(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 interfaces array.
        object[] bases = {
        ConvertFullName(cm, "System.IDisposable"), 
        ConvertFullName(cm, "System.IComparable")
        };

        // Create a new class.
        cm.AddInterface("TestInterface", projItem.Name, -1, bases, 
            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;
}

权限

另请参见

参考

CodeModel2 接口

CodeModel2 成员

AddInterface 重载

EnvDTE80 命名空间

其他资源

如何:编译和运行自动化对象模型代码示例

使用代码模型查找代码 (Visual Basic)

使用代码模型查找代码 (Visual C#)