简体   繁体   English

删除程序集:AssemblyInfo.cs中的零件

[英]Removing assembly: part from AssemblyInfo.cs

I'm using Rosyln to manipulate the AssemblyInfo.cs as part of a tool I'm making and I'm having trouble figuring out the right way to remove this line from a file: 我正在使用Rosyln作为我正在制作的工具的一部分来操作AssemblyInfo.cs ,但在寻找正确的方法从文件中删除此行时遇到了麻烦:

[assembly: DeploymentLocation("\\\\someserver\\folder")]

I can also have this: 我也可以这样:

[assembly: DeploymentLocation("\\\\someserver\\folder"), DeploymentLocation("\\\\someotherserver\\folder")]

What I want to do is find those lines, remove them, and recreate them. 我要做的是找到这些行,将其删除,然后重新创建它们。 Recreating them is fine, but removing them is the problem. 重新创建它们很好,但是删除它们是问题。

I can find those DeploymentLocation pieces like this: 我可以找到以下那些DeploymentLocation片段:

var attr = syntaxRoot.DescendantNodes().OfType<AttributeSyntax>().Where(a => a.Name.ToString() == "DeploymentLocation");

And then I do something like this: 然后我做这样的事情:

if (attr?.Any() == true)
{
    // remove existing attributes...
    syntaxRoot = syntaxRoot.RemoveNodes(attr, SyntaxRemoveOptions.KeepNoTrivia);
}

But this ends up leaving this in my file: 但这最终将其保留在我的文件中:

[assembly: ]

Which is, obviously, not what I want. 显然,这不是我想要的。 How do I find the [assembly: ] bit and remove that along with the DeploymentLocation attribute? 如何找到[assembly: ]位并将DeploymentLocation属性一起删除? And obviously I don't want to remove all [assembly: ] lines as there's a bunch of lines that are boilerplate stuff that needs to stay. 显然,我不想删除所有的[assembly: ]行,因为有一堆需要保留的样板行。

The assembly bit doesn't seem to be considered a Parent (the Parent ) of my tags in attr are the whole document. assembly位似乎并未视为attr中我的标签的ParentParent )是整个文档。 So what do I need to do to walk back to the assembly piece? 那么,我该怎么做才能回到assembly上呢? What is that bit even called? 那叫什么呢?

Ok, I figured this out. 好的,我知道了。 You need to select the AttributeListSyntax , which is basically the set of square brackets [] that contains an AttributeSyntax node matching what I want. 您需要选择AttributeListSyntax ,它基本上是方括号[]的集合,其中包含与我想要的匹配的AttributeSyntax节点。 Then I can remove that whole thing which would include the AttributeTargetSyntax (the assembly: part) which is a sibling to the AttributeSyntax 然后,我可以删除整个内容,其中包括AttributeTargetSyntaxassembly:部件),它是AttributeSyntax的同级对象

var attr = syntaxRoot.DescendantNodes().OfType<AttributeListSyntax>()
    .Where(a => a.DescendantNodes().OfType<AttributeSyntax>()
    .Any(s => s.Name.ToString() == "DeploymentLocation"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM