beakona / AutoInterface

C# interface-to-member source generator
MIT License
76 stars 10 forks source link

ObsoleteAttribute inheritance #18

Closed amoraller closed 11 months ago

amoraller commented 1 year ago

Nice source generator. Thanks.

If i want delegate implementation to interface with Obsolete methods, i have compilation warnings CS0618. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0618?f1url=%3FappId%3Droslyn%26k%3Dk(CS0618)

For example, i want delegate Nlog.ILogger implmentation.

NLog source coge:

    /// <summary>
    /// Writes the diagnostic message at the <c>Debug</c> level using the specified parameters.
    /// </summary>
    /// <param name="message">A <see langword="string" /> containing format items.</param>
    /// <param name="args">Arguments to format.</param>
    [MessageTemplateFormatMethod("message")]
    void Debug([Localizable(false)][StructuredMessageTemplate] string message, params object[] args);

    /// <summary>
    /// Writes the diagnostic message and exception at the <c>Debug</c> level.
    /// </summary>
    /// <param name="message">A <see langword="string" /> to be written.</param>
    /// <param name="exception">An exception to be logged.</param>
    /// <remarks>This method was marked as obsolete before NLog 4.3.11 and it may be removed in a future release.</remarks>
    [Obsolete("Use Debug(Exception exception, string message) method instead. Marked obsolete with v4.3.11")]
    [EditorBrowsable(EditorBrowsableState.Never)]
    void Debug([Localizable(false)] string message, Exception exception);

    /// <summary>

Generated code:

    void NLog.ILogger.Debug(string message, params object[] args) => this._nlogImpl.Debug(message, args);

    void NLog.ILogger.Debug(string message, System.Exception exception) => this._nlogImpl.Debug(message, exception);

image

I think, we want nesting Obsolete attribute.

beakona commented 1 year ago

As I understood the real cause of the problem, the ObsoleteAttribute has Inherited=false and a copy of that attribute should be added to the generated source so compiler would not generate warning. It seems the correct aproach would be to add an additional attribute PassthroughAttributes="Obsolete" (that is the list of attributes). In that case Obsolete would be forwarded by default, but anyone could change that behaviour for other attributes.

I don't have enough free time at the moment and will try to solve this and another issue by the end of this year.

beakona commented 11 months ago

I've made nuget prerelease package 1.0.30-pre that should fix this issue.

amoraller commented 11 months ago

thanks. It work for me.