Skip to content

Fully qualify references to generated interfaces#1

Draft
simonmckenzie wants to merge 7 commits intoChristianSauer:masterfrom
simonmckenzie:feature/fully-qualify-references-to-generated-interfaces
Draft

Fully qualify references to generated interfaces#1
simonmckenzie wants to merge 7 commits intoChristianSauer:masterfrom
simonmckenzie:feature/fully-qualify-references-to-generated-interfaces

Conversation

@simonmckenzie
Copy link
Copy Markdown

This is a copy of codecentric#89, reopening after the sunsetting of that project.

This addresses an issue where, when a class references a generated interface, those references are not fully qualified, resulting in an interface that doesn't compile. See codecentric#87.

The change is to precalculate the list of interface names that will be generated, and, during symbol string generation, replace unrecognised symbols with generated names where a single unambiguous match can be made between the symbol and the list of interfaces being generated.

For the example provided in codecentric#87, the code will now generate this output:

//--------------------------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------------------------

namespace Test
{
    [global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
    public partial interface ITest1
    {
        /// <inheritdoc cref="Test.Test1.Get(ITest2)" />
        void Get(global::Test.Tests.ITest2 test);
        
    }
}


//--------------------------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------------------------

namespace Test.Tests
{
    [global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
    public partial interface ITest2
    {
    }
}

@simonmckenzie simonmckenzie force-pushed the feature/fully-qualify-references-to-generated-interfaces branch 2 times, most recently from 8e7605b to 640a9bb Compare April 23, 2026 22:17
@simonmckenzie simonmckenzie marked this pull request as draft April 23, 2026 22:19
This addresses an issue where, when a class references a generated interface, those references are not fully qualified, resulting in an interface that doesn't compile.

The change is to precalculate the list of interface names that will be generated, and, during symbol string generation, replace unrecognised symbols with generated names _where a single unambiguous match can be made between the symbol and the list of interfaces being generated_.
- Change approach to use `ToDisplayParts`, which removes the need for regex parsing of generated code
- Fix bug in `ReplaceWithInferredInterfaceName` where dots weren't being escaped
- Add tests to ensure partially qualified references will be resolved correctly
…class; refactor

This centralises the rendering logic plus simplifies the implementation to use `ITypeSymbol.WithNullableAnnotation` and hands the rendering off to the default `ToDisplayString` implementation
@simonmckenzie simonmckenzie force-pushed the feature/fully-qualify-references-to-generated-interfaces branch from 640a9bb to 44b679e Compare April 23, 2026 22:22
@simonmckenzie simonmckenzie changed the title Feature/fully qualify references to generated interfaces Fully qualify references to generated interfaces Apr 23, 2026
@simonmckenzie simonmckenzie marked this pull request as ready for review April 23, 2026 22:35
@simonmckenzie simonmckenzie marked this pull request as draft April 23, 2026 22:36
@simonmckenzie
Copy link
Copy Markdown
Author

simonmckenzie commented Apr 23, 2026

Hi @ChristianSauer ,

I see the RoslynExtensions class is now marked with an explicit comment:

Source: https://github.com/dominikjeske/Samples/blob/main/SourceGenerators/HomeCenter.SourceGenerators/Extensions/RoslynExtensions.cs

Does this mean any customisations I want to apply should be moved into a different extensions class, to keep RoslynExtensions as a clean direct copy...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant