Skip to content

How to add Compile Update item to csproj? #5244

Open
@ghost

Description

I'm trying to use Microsoft Build SDK to add a Compile Update item to the SDK style csproj file. What I need is to basically generate an XML like the following:

<Compile Update="Class1.Generated.cs">
  <DependentUpon>Class1.cs</DependentUpon>
</Compile>

However, I couldn't find an easy way to do this. If I simply use ProjectItem.SetMetadataValue, I get the following exception:

System.InvalidOperationException: 'Cannot modify an evaluated object originating in an imported file "C:\Program Files\dotnet\sdk\3.1.201\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.props".'

And if I try to use APIs like Project.AddItem, I have to go through this:

var item = project.AddItem("Compile", "Class1.Generated.cs", new []
{
    new KeyValuePair<string, string>("DependentUpon", "Class1.cs")
}).Single();
item.Xml.Include = null;
item.Xml.Update = "Class1.Generated.cs";

Because Project.AddItem requires an "unevaluated include".

Am I missing something? Is there a better way to do this?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions