CodeAttribute2.AddArgument(String, Object, Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
向特性添加参数。
EnvDTE80::CodeAttributeArgument AddArgument(std::wstring const & Value, winrt::Windows::Foundation::IInspectable const & Name, winrt::Windows::Foundation::IInspectable const & Position);
[System.Runtime.InteropServices.DispId(201)]
public EnvDTE80.CodeAttributeArgument AddArgument (string Value, object Name, object Position);
[<System.Runtime.InteropServices.DispId(201)>]
abstract member AddArgument : string * obj * obj -> EnvDTE80.CodeAttributeArgument
Public Function AddArgument (Value As String, Optional Name As Object, Optional Position As Object) As CodeAttributeArgument
参数
- Value
- String
必需。 默认值为 0。将在其后添加新元素的元素。 如果该值为 CodeElement ,则在参数 CodeElement后添加新元素。 如果值是 Long 数据类型,则指示在其后添加新元素的元素。 因为集合从 1 开始计数,所以传递 0 指示应将新元素放置在集合的开始处。 值 -1 表示将参数放在结尾处。
- Name
- Object
必需。 参数的值。
- Position
- Object
可选。 如果参数是命名参数,则该命名参数包含此参数的名称。
返回
- 属性
示例
下面的示例在当前类中创建新的命名空间和属性,并列出属性的一些属性。
public void CreateClassAndAttrib(DTE2 applicationObject)
{
// Before running, load or create a project.
FileCodeModel2 fcm2 = GetFileCodeModel(applicationObject);
CodeAttribute2 cmAttribute;
CodeClass2 cmClass;
String msg = null;
if (fcm2 != null)
{
CodeNamespace cmNamespace;
// Try to create a new namespace.
try
{
cmNamespace = fcm2.AddNamespace("CMNamespace", -1);
// If successful, create the other code elements.
if (cmNamespace != null)
{
cmClass = (CodeClass2)cmNamespace.AddClass("ANewClass",
-1, null, null, vsCMAccess.vsCMAccessPrivate);
cmAttribute = (CodeAttribute2)cmClass.AddAttribute
("NewAttribute", "AttributeValue", -1);
msg += "# of Arguments: " + cmAttribute.Arguments.Count
+ Environment.NewLine;
MessageBox.Show(msg);
cmAttribute.AddArgument("NewAddedValue", null, null);
msg += "# of Arguments: " + cmAttribute.Arguments.Count
+ Environment.NewLine;
MessageBox.Show(msg);
}
else
{
MessageBox.Show("Cannot continue - no filecodemodel
available.");
}
}
catch (Exception ex)
{
MessageBox.Show("ERROR: " + ex);
}
}
}
public FileCodeModel2 GetFileCodeModel(DTE2 applicationObject)
{
// Returns the FileCodeModel object of the active
// window.
TextWindow txtWin =
(TextWindow)applicationObject.ActiveWindow.Object;
FileCodeModel2 fcm2;
if (txtWin != null)
{
try
{
fcm2 = (FileCodeModel2)txtWin.Parent.
ProjectItem.FileCodeModel;
return fcm2;
}
catch (Exception ex)
{
MessageBox.Show("ERROR: " + ex);
return null;
}
}
else
return null;
}
注解
备注
在赋值后,代码属性参数值不会保留在内存中 Visual Studio ,因此,在对 code 特性参数进行后续更新时,可能会也可能无效。 也就是说,后续参数访问可能返回 E_FAIL 或完全不同的值。 但 (影响元素的子级的任何内容,则不会出现此问题。 )
由于这种不确定的行为,您应在更改之前检索参数的值。 例如,如果您在代码中设置了代码属性参数(如),则 myAttrArg.Value = """a first value""" 应在更新之前显式引用它(如),然后 myAttrArg = myAttr.Arguments.Item("first value") 分配新的值 myAttrArg.Value = """a second value""" 。 这样做可以确保更改正确的参数。
此外,在进行某些类型的编辑之后,代码模型元素(例如类、结构、函数、特性、委托等)的值可能是不确定的,这意味着无法依赖于它们的值始终保持不变。 有关详细信息,请参阅 "代码模型元素值在 使用代码模型查找代码时 可能发生变化" (Visual Basic) "。