c# - 使用 MSBUILD API 更新 *.CSPROJ

标签 c# linq msbuild

基于问题:Reading a *.CSPROJ file in C#

我有代码可以从 *.csproj 文件中提取一些属性,代码如下:

Project project = new Project();

var Property001=
            from pg in project.PropertyGroups.Cast<BuildPropertyGroup>()
            from item in pg.Cast<BuildProperty>()
            where item.Name == "Property001"
            select item.Value.ToString();

这工作正常,但下一个问题是我如何也使用 LINQ 更新属性?

最佳答案

您可以使用 LINQ 获取属性项 - 而不仅仅是值 - 以更新:

var Property001item =
        (from pg in project.PropertyGroups.Cast<BuildPropertyGroup>()
        from item in pg.Cast<BuildProperty>()
        where item.Name == "Property001"
        select item).FirstOrDefault();
if (Property001item != null)
{
    Property001item.Value = "MyNewValue";
}

关于c# - 使用 MSBUILD API 更新 *.CSPROJ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4993741/

相关文章:

asp.net - Linq 查询返回同一行 12 次

c# - 使用 Visual Studio 生成错误 "MSB4006: There is a circular dependency in the target dependency graph involving target "生成“。”

c# - 如何在 LINQ 中获取所需的集合

c# - 以编程方式设置文本 block 边距

c# - 使用 Linq 将数据分区到数组中

c# - 生成通用列表的组合

c# - 尝试通过用户点击来自动化 C# 解决方案的构建过程

c# - 未找到 MSBuild 社区任务

c# - 为 HTTP 监听器设置 'Access-Control-Allow-Origin'

c# - IIS - 拒绝访问路径