Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning fix using MemberNullAttribute causes accessibility level error. #4763

Closed
kvansaders opened this issue Feb 5, 2024 · 10 comments
Closed

Comments

@kvansaders
Copy link

          > I think a better solution is to generate
 public string BaseUrl
 {
     get { return _baseUrl; }
     [System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_baseUrl))]
     set
     {
         _baseUrl = value;
         if (!string.IsNullOrEmpty(_baseUrl) && !_baseUrl.EndsWith("/"))
             _baseUrl += '/';
     }
 }

To let the compiler null analysis know that the property setter sets the field _baseUrl.

In my netstandard2.0 project, this gives: 'MemberNotNullAttribute' is inaccessible due to its protection level.

Originally posted by @kvansaders in #4704 (comment)

@Vogel612
Copy link

Vogel612 commented Feb 6, 2024

I'm seeing the same issue as the other commenter on that issue, where the build completely breaks with the following error message:

Error	CS0234	The type or namespace name 'MemberNotNullAttribute' does not exist in the namespace 'System.Diagnostics.CodeAnalysis' (are you missing an assembly reference?)	

as it is, 14.0.3 breaks netstandard compat from what I can tell

@Wholteza
Copy link

Wholteza commented Feb 6, 2024

I'm seeing the same issue as well, trying to use the generated C# client in a net472 project.
Downgrading to 14.0.2 removed the attribute from the generated class.
That works as a workaround for me at the moment.

@ImoutoChan
Copy link

ImoutoChan commented Feb 6, 2024

The same issue on my side, MemberNotNull shouldn't be emitted when targeting netstandard2.0

@nessuarez
Copy link

nessuarez commented Feb 9, 2024

The same issue for targeting WinX86 runtime in a .Net framework 4.5.2 project

@jstun
Copy link

jstun commented Feb 12, 2024

The same issue on my side as well. Caused by NSwag toolchain v14.0.3.0.

@melucas
Copy link

melucas commented Feb 20, 2024

The same issue with NSwag v14.0.3.0 for a .Net 4.8 project.

@jochenwezel
Copy link

The feature System.Diagnostics.CodeAnalysis.MemberNotNull introduced with .NET 5 as MS documentation declares at https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.membernotnullattribute?view=net-8.0

So, the code line

            [System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_baseUrl))]

should be as follows to solve the compilation error

#if NET5_0_OR_GREATER
            [System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_baseUrl))]
#endif

@rwb196884
Copy link

I need to multi-target net40 and this makes 14 unusable, have to use 13.

@jochenwezel
Copy link

jochenwezel commented Mar 20, 2024

If you render the code as provided into the generated document, you don't need to multi-target your solution itself. Just the generated output has to provide the 2 additional lines for compiler directives

@Vogel612
Copy link

Forward duplicated by #4833, fixed since 14.0.6

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

No branches or pull requests

10 participants