net_automatic_interface icon indicating copy to clipboard operation
net_automatic_interface copied to clipboard

System.Diagnostics.CodeAnalysis Attributes not pulled into interface

Open leorg99 opened this issue 4 months ago • 4 comments

For example:

[GenerateAutomaticInterface]
public class Foo : IFoo
{
    public bool TryGetValue(string blah, [NotNullWhen(true)] out string? value)
    {
        value = null;
        return true;
    }
}
//--------------------------------------------------------------------------------------------------
// <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>
//--------------------------------------------------------------------------------------------------

#nullable enable
namespace Test
{
    [global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
    public partial interface IFoo
    {
        /// <inheritdoc cref="Test.Foo.TryGetValue(string, out string?)" />
        bool TryGetValue(string blah, out string? value); <--- missing NotNullWhen attribute
        
    }
}
#nullable restore

leorg99 avatar Aug 25 '25 18:08 leorg99

I am not sure I understand you? What's the problem?

ChristianSauer avatar Nov 05 '25 06:11 ChristianSauer

The generated interface is missing the [NotNullWhen(true)] attribute for the out parameter when the class method specifies it. The generator should pull this attribute into the generated interface.

Perhaps a way to do this is to check if the parameters in the method have attributes and include them in the generated interface.

leorg99 avatar Nov 05 '25 15:11 leorg99

I am unsure if all method-attributes from a class can also be applied to an interface method. I will look into this

ChristianSauer avatar Nov 14 '25 08:11 ChristianSauer

I think we can start with just the attributes in the System.Diagnostics.CodeAnalysis namespace.

leorg99 avatar Nov 14 '25 15:11 leorg99