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

定义

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

EnvDTE::CodeAttribute AddAttribute(std::wstring const & Name, std::wstring const & Value, winrt::Windows::Foundation::IInspectable const & Position);
[System.Runtime.InteropServices.DispId(36)]
public EnvDTE.CodeAttribute AddAttribute (string Name, string Value, object Position);
[<System.Runtime.InteropServices.DispId(36)>]
abstract member AddAttribute : string * string * obj -> EnvDTE.CodeAttribute
Public Function AddAttribute (Name As String, Value As String, Optional Position As Object) As CodeAttribute

参数

Name
String

属性名。

Value
String

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

Position
Object

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

如果值为零,新元素添加到集合开头(默认情况);如果值为 -1,则添加到集合末尾。

返回

CodeAttribute

CodeAttribute 对象。

属性

示例

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

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");  
}  

注解

备注

在进行了某些类型的编辑之后,代码模型元素(例如类、结构、函数、特性、委托等)的值可能变为非确定性的,这意味着不能确定它们的值始终保持不变。

适用于