'Decorating AdditionalText-derived class for "build_metadata.additionalfiles.some_property"

I'm building a source generator that catches csv files marked with the name of the generator.

Having

<AdditionalFiles Include="File1.csv">
      <Generator>GeneratorX</Generator>
</AdditionalFiles>

the generator filters for such files:

context.AnalyzerConfigOptions.GetOptions(file).TryGetValue("build_metadata.additionalfiles.generator", out string? generatorString);
if (generatorString == nameof(GeneratorX)) 
{
    yield return file;
}

This is inspired by how https://github.com/dotnet/roslyn-sdk/blob/main/samples/CSharp/SourceGenerators/SourceGeneratorSamples/CsvGenerator.cs works.

I'm having trouble creating an AdditionalText object - for unit testing - that has this extra 'generator' metadata. The generator skips the provided AdditionalText.

public class CustomAdditionalTextFromString : AdditionalText 
(...)

[TestMethod]
public void Test()
{
  ...

  AdditionalText additional = new CustomAdditionalTextFromString(...);

  // Create the driver that will control the generation, passing in our generator
  GeneratorDriver driver = CSharpGeneratorDriver.Create(
    generators: new[] { testedGenerator },
    additionalTexts: new[] { additional });

How to decorate/modify public class CustomAdditionalTextFromString : AdditionalText so that AnalyzerConfigOptions.GetOptions(file).TryGetValue("build_metadata.additionalfiles.some_property" catches some_property? I naively though that maybe adding a public property will do it, but it does not :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source