gpt4 book ai didi

c# - 使用 filecodemodel 添加属性

转载 作者:太空宇宙 更新时间:2023-11-03 16:42:21 25 4
gpt4 key购买 nike

我正在尝试使用插件向类添加一些属性。我可以让下面的代码工作,除了我想要每个包含在 [] 中的新行上的属性。我该怎么做?

if (element2.Kind == vsCMElement.vsCMElementFunction)
{
CodeFunction2 func = (CodeFunction2)element2;
if (func.Access == vsCMAccess.vsCMAccessPublic)
{
func.AddAttribute("Name", "\"" + func.Name + "\"", 0);
func.AddAttribute("Active", "\"" + "Yes" + "\"", 0);
func.AddAttribute("Priority", "1", 0);
}
}

属性被添加到公共(public)方法中,如

[Name("TestMet"), Active("Yes"), Priority(1)]

如我所愿

[Name("TestMet")]
[Active("Yes")]
[Priority(1)]
public void TestMet()
{}

另外,我如何添加一个没有任何值的属性,例如 [PriMethod]。

最佳答案

您正在使用的方法的签名:

CodeAttribute AddAttribute(
string Name,
string Value,
Object Position
)
  1. 如果您不需要该属性的值,请使用 String.Empty第二个参数字段
  2. 第三个参数用于Position .在您的代码中,您为 0 设置了三次此参数,VS认为这是同一个属性。所以使用一些索引,比如:

func.AddAttribute("Name", "\"" + func.Name + "\"", 1);<br/>
func.AddAttribute("Active", "\"" + "Yes" + "\"", 2);<br/>
func.AddAttribute("Priority", "1", 3);

如果你不想使用索引,使用-1 value - 这将在集合末尾添加新属性。

More on MSDN

关于c# - 使用 filecodemodel 添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7126097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com