Код: Выделить всё
public class XmlCommentIncrementalGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var members = context.CompilationProvider
.Select((compilation, _) => {
var names = compilation.GlobalNamespace.GetNamespaceMembers()
.Where(x => x.Name.Contains("SourceGenTest") || x.Name.Contains("SomeLib")) //gets the namespaces
.SelectMany(x=> x.GetMembers()) //gets classes
.Select(x => $"{x.Name} DSR:{x.DeclaringSyntaxReferences.FirstOrDefault()}");
return names.ToImmutableArray();
});
context.RegisterSourceOutput(members, (ctx, array) => {
string output = "//" + string.Join("\n//", array);
ctx.AddSource("Output.g.cs", SourceText.From(output, Encoding.UTF8));
}
);
}
}
- SourceGenTest — базовый проект, консольное приложение, имеет ссылку на SourceGen и SomeLib
- SourceGen – где находится sg
- SomeLib — какая-то библиотека, не имеет ссылок. На него ссылается SourceGenTest
Код: Выделить всё
//ClassInBaseProj DSR:Microsoft.CodeAnalysis.CSharp.SimpleSyntaxReference
//ClassInLib DSR:
Код: Выделить всё
ClassInBaseProj
Можно ли получить DeclaringSyntaxReferences даже для классов из указанного проекта?
Каковы другие варианты?
В конце концов я пытаюсь собрать комментарии XML из свойств а также их значения по умолчанию. Я решил, что для этого мне понадобится DeclaringSyntaxReferences, но если есть другое решение, не стесняйтесь поделиться им.
Полный исходный код.
Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/792 ... ctreferenc