CodeDelegate2.AddAttribute 方法 (String, String, Object)

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

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

语法

声明
Function AddAttribute ( _
    Name As String, _
    Value As String, _
    Position As Object _
) As CodeAttribute
CodeAttribute AddAttribute(
    string Name,
    string Value,
    Object Position
)
CodeAttribute^ AddAttribute(
    String^ Name, 
    String^ Value, 
    Object^ Position
)
abstract AddAttribute : 
        Name:string * 
        Value:string * 
        Position:Object -> CodeAttribute 
function AddAttribute(
    Name : String, 
    Value : String, 
    Position : Object
) : CodeAttribute

参数

  • Value
    类型:System.String
    必选。特性的值,该值可以是具有参数的属性的逗号分隔参数列表。
  • Position
    类型:System.Object
    可选。默认值 = 0。将在其后添加新元素的代码元素。
    如果 Value 为 Long 数据类型,则 AddAttribute 指示在哪个元素后添加新元素。
    因为集合从 1 开始计数,所以传递 0 指示应将新元素放置在集合的开始处。值为 -1 表示应将元素放在结尾处。

返回值

类型:EnvDTE.CodeAttribute
一个 CodeAttribute 对象。

实现

CodeDelegate.AddAttribute(String, String, Object)

备注

如果该特性已存在,则 AddAttribute 将添加另一个特性。 这对于代码模型后面的特性或语言可能是不正确的。

提示

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

示例

在运行此示例之前,加载一个具有某个类的项目,并插入下行以及代码示例。 在添加了该行和代码示例之后,单击 Delegate 行将插入点放在该行处,然后运行该示例。

[VisualBasic]

Delegate Sub MySubDelegate(ByVal x As Integer)

[C#]

public delegate void MySubDelegate(int x);

[VisualBasic]

Public Sub addDelegateAttribExample(ByVal dte As DTE2)
    Try
        ' Retrieve the CodeClass at the insertion point.
        Dim sel As TextSelection = _
          CType(dte.ActiveDocument.Selection, TextSelection)
        Dim del As CodeDelegate2 = _
          CType(sel.ActivePoint.CodeElement( _
        vsCMElement.vsCMElementDelegate), CodeDelegate2)

        ' Add a new attribute to the delegate.
        del.AddAttribute("someNewAttribute", "someValue", -1)
    Catch ex As System.Exception
        MsgBox(ex.ToString)
    End Try
End Sub

.NET Framework 安全性

请参见

参考

CodeDelegate2 接口

AddAttribute 重载

EnvDTE80 命名空间

其他资源

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

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

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