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

error CS0122: 'ThisAssembly' is inaccessible due to its protection level #449

Closed
MSThug opened this issue Mar 13, 2020 · 3 comments
Closed

Comments

@MSThug
Copy link

MSThug commented Mar 13, 2020

Hi there I'm on a project that uses Nerdbank.GitVersioning 3.1.68. After a number of changes updating our WPF project to .NET Core 3.1 at some point we started to see the follow error.

error CS0122: 'ThisAssembly' is inaccessible due to its protection level

To be clear this had been working. But at some point we changed our project SDK from "MSBuild.SDK.Extras" to "Microsoft.NET.Sdk.WindowsDesktop".

Of course this is because we use some of the properties from the ThisAssembly class in our code. After a number of attempts to determine the cause I finally tried defining ThisAssembly in our code as seen below.

internal static partial class ThisAssembly { internal const int IgnoreMe = 0; }

This changed all the errors all to something along the lines of the error below.

error CS0117: 'ThisAssembly' does not contain a definition for 'AssemblyVersion'

I only found a couple references to the first error online but they seemed to work around the issue by disabling GitVersioning and one eluded to the timing of when ThisAssembly is created. That would certainly explain the two different errors and the fact that I see the errors in VS after building but they very quickly disappear in the Error List panel when I navigate to the files with the errors. However, they can always be found in the build logs.

Use of GitVersioning is seems pretty straight forward and the only significant csproj file change was the SDK change. All other changes were code so I doubt they could affect this.

@AArnott
Copy link
Collaborator

AArnott commented Mar 13, 2020

I believe the reason you're struggling is due to an open issue in the WPF build SDK, where it doesn't respect PackageReference in its "inner build".

Your issue sounds like a duplicate of #404, though the symptom is slightly different because your WPF project references another project that also uses NB.GV, whereas in #404 that isn't the case.

Basically in WPF's inner build, NB.GV doesn't get a chance to run and thus doesn't create the ThisAssembly class. But you have a project reference to another assembly that does define it, so C# thinks references in your code to ThisAssembly must be referring to that one. Since this class is always defined as internal, you then get an error that you can't access it, which is misleading because you actually want to be accessing your own internal one that doesn't exist.

The good news though is there are workarounds for this. Check out #404 (and links) to learn more.

@AArnott AArnott closed this as completed Mar 13, 2020
@MSThug
Copy link
Author

MSThug commented Mar 13, 2020

That did the trick. Thanks!

@LevYas
Copy link

LevYas commented Apr 13, 2021

This issue is the first in Google results, so instead of the long chain of links I'll add a shortcut: microsoft/CsWin32#7 (comment)

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

3 participants