CodeEvent.AddAttribute 方法

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

命名空间:  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

参数

  • Name
    类型:String

    属性名。

  • Value
    类型:String

    特性的值,该值可以是以逗号分隔的参数化属性的参数列表。

  • Position
    类型:Object

    可选。 在其后添加新元素的元素位置。

    如果此值为 0,则在集合的开始位置(默认设置)添加新元素;如果值为 -1,则在结束位置添加新元素。

返回值

类型:CodeAttribute
一个 CodeAttribute 对象。

备注

备注

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

示例

下面的示例演示如何使用 AddAttribute 方法。

测试此属性:

  1. 打开目标项目并通过将光标放在事件对象上来选择代码事件元素。

  2. 运行外接程序。

public static void AddAttribute(EnvDTE80.DTE2 dte)
{
    TextSelection objTextSel;
    EnvDTE80.CodeEvent codeEvent;
    objTextSel = (TextSelection)dte.ActiveDocument.Selection;
    codeEvent = (EnvDTE80.CodeEvent)objTextSel.ActivePoint.get_CodeElement(vsCMElement.vsCMElementEvent);

    codeEvent.AddAttribute("Obsolete", "\"NewAttribute\", true", 1);
    string str = "";
    foreach (CodeElement ce in codeEvent.Attributes)
        str += "\n" + ce.FullName;
    MessageBox.Show("\nAddAttribute method: " +
                    str, "Testing CodeEvent");
}

.NET Framework 安全性

请参阅

参考

CodeEvent 接口

EnvDTE80 命名空间

其他资源

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

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

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