microsoft / microsoft/CsWin32

Error CS0122: '{0}' is inaccessible due to its protection level

Open
#1,671 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C#
Stars
2.5k
Forks
124
Avg merge
1d 3h
Merged PRs (30d)
9

Description

Actual behavior

Build error for any type generated by CsWin32, if a referenced assembly also contains the type as internal! Example error:

  • error CS0122: 'HRESULT' is inaccessible due to its protection level.

Expected behavior

When using any type from CsWin32 that is generated into the local assembly - even if the type also exist in a referenced assembly - I am expecting the generated type to be used.

Repro steps

  1. NativeMethods.txt content:
use pointers.
COLORREF
IsZoomed
DefWindowProc
SetWindowPos
GetWindowRect
PostMessage
DwmSetWindowAttribute
SHAppBarMessage
S_OK
E_INVALIDARG
HRESULT_FROM_WIN32
MAKELONG
LOWORD
HIWORD

  1. NativeMethods.json content (if present): Not present

  2. Any of your own code that should be shared?

Context
  • CsWin32 version: 0.3.269+368685089b.RR
  • Win32Metadata version (if explicitly set by project):
  • Target Framework: net48
  • LangVersion (if explicitly set by project): 13.0
  • OS: Win11 ARM

The repo can be found below:

[!NOTE]
I suggest downloading this single project (not the entire solution) to easily build:

https://github.com/rezanid/xrmtools/blob/vsix-hybrid/src/XrmTools.Shell/XrmTools.Shell.csproj

Additional Context

I also tried building outside Visual Studio using dotnet build. This command successfully builds the project. Then I added the following piece to the project and tested again. This time also dotnet build fails similarly.

  <PropertyGroup>
    <CsWin32RunAsBuildTask>true</CsWin32RunAsBuildTask>
  </PropertyGroup>

I guess the source generator has logic to detect if the type already exists, but it does not distinguish between type exist in the current assembly or a referenced one or this logic doesn't work when directly building with MSBuild and only via dotnet build.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the XrmTools.Shell.csproj reproduction and its NativeMethods.txt input, then compare the normal build with dotnet build when CsWin32RunAsBuildTask is enabled. Trace how generated types are checked against referenced assemblies, and verify completion by rebuilding the project without the CS0122 error while still using the locally generated types.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
build-system, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.