RSCG–AMS – About My software –Reading csproj– part 6
Now it is time to put some more data – like authors and version. I have read a lot ( and tried a lot) about CompilerVisibleProperty and CompilerVisibleItemMetadata ( see https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md ) . However, I was unable to get the data ( Authors and Version) from there .
So this is what I was get, to read the csproj near the program:
private ItemsFromCSPROJ TryGetPropertiesFromCSPROJ(GeneratorExecutionContext context) { var ret= new ItemsFromCSPROJ(); try { var dirFolder = ((dynamic)(context.Compilation)).Options?.SourceReferenceResolver?.BaseDirectory; if (string.IsNullOrWhiteSpace(dirFolder)) return ret; var file = Directory.GetFiles(dirFolder, "*.csproj"); if (file.Length != 1) throw new ArgumentException($"find files at {dirFolder} :{file.Length} "); var xmldoc = new XmlDocument(); xmldoc.Load(file[0]); XmlNode node; node = xmldoc.SelectSingleNode("//Authors"); ret.Authors = node?.InnerText; node = xmldoc.SelectSingleNode("//Version"); ret.Version = node?.InnerText; return ret; } catch(Exception ) { //maybe log warning? return ret; } }
Next time I will show how it looks
Leave a Reply