Code Monkey home page Code Monkey logo

Comments (7)

chucker avatar chucker commented on May 18, 2024 1

(Thanks for your answer. Will look into those!)

from nullable.

AArnott avatar AArnott commented on May 18, 2024 1

Check out microsoft/CsWin32#7 for a discussion of this and how the latest .NET SDK fixed their bug so Nullable should Just Work going forward.

from nullable.

manuelroemer avatar manuelroemer commented on May 18, 2024

Hey, sorry to hear that you are having this issue. I've done some testing/browsing and can reproduce it locally (Project Download Here - open MainWindow.xaml.cs for the method triggering the problem).

It seems that the issue arises from the way WPF projects are compiled. There are some GitHub issues which may provide more insights into why the problem occurs:

To summarize the issue: During the build, WPF creates a temporary project (for compiling XAML, I believe). In this temporary project, the NuGet packages are not imported, hence the build error that the attributes could not be found. This problem also affects other source code only packages similar to Nullable.

Unfortunately I am currently unsure how this might be fixed. Maybe there's a chance to develop a workaround, but ultimately, the issue should be fixed at the root, i.e. in the WPF build process.

Nontheless, I don't want to leave you hanging - maybe there are other ways to solve your problem until a real solution arrives for this issue. Here are some suggestions:

  1. You might want to have a look at the https://github.com/tunnelvisionlabs/ReferenceAssemblyAnnotator project. They also offer a way to include the nullable attributes in a legacy projects. I haven't tried, but perhaps their approach works better?
  2. This is nasty after all the work you have put into upgrading your project, but maybe you can try going back to a non-SDK style project. Again, not sure if this solves the problem, but it might be worth a try.
  3. [The Approach I'd Use] You could not use the Nullable package for WPF projects and instead manually define the attributes using a shared project. This should work with any project configuration. This is also not ideal since you lose automatic package updates, but realistically, there won't be many since the .NET team doesn't periodically create new attributes. If anything, #9 will be added soon-ish, but that's it for the next year(s).

If you (or anyone else) has any ideas how the problem can be worked around from the perspective of the Nullable package, feel free to share your ideas!

from nullable.

chucker avatar chucker commented on May 18, 2024

So, I tried approach 1, and… they have a similar issue (which sounds… more fixable?):

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

It looks like someone else already discovered that: tunnelvisionlabs/ReferenceAssemblyAnnotator#81

2 seems… not fun.

I might be able to live with 3. But now I'm tempted by the additional features in the project mentioned in 1…

from nullable.

manuelroemer avatar manuelroemer commented on May 18, 2024

Alright, thanks for testing! I'm not sure about the CS0122 being more fixable. On first sight, the only solution seems to be to make the attributes public, but that should never be done, because it will quickly lead to naming conflicts when another referencing project also declares these attributes. Hence why they are declared as internal. (The only exception where public attributes are okay-ish is a project which is not published - there you have full control over it.)

now I'm tempted by the additional features in the project mentioned in 1…

I totally recommend using ReferenceAssemblyAnnotator instead of Nullable then! It is indeed an awesome project.
Nullable is kept lightweight on purpose, i.e. I only want to provide the attributes and nothing else. Using an analogy, Nullable is, so to say, the lightweight code editor while ReferenceAssemblyAnnotator is a fully featured IDE - sometimes the first is enough, but for larger tasks/projects, the additional features are preferable. 😄

If they cannot fix the CS0122 error which you posted above, you might even be able to combine ReferenceAssemblyAnnotator with Option 3 above, i.e. you could manually create the NullableAttributes.cs file in your project to have access to the attributes and disable RAA's attribute generation in your cs project: <GenerateNullableAttributes>false</GenerateNullableAttributes>. Not tested, but this might work just fine for your project.

from nullable.

chucker avatar chucker commented on May 18, 2024

Nullable is kept lightweight on purpose, i.e. I only want to provide the attributes and nothing else.

Yeah, makes sense to me. This is the one I ran into, but I think I want the "IDE" approach for my use case.

They did figure out a fix, which appears to work for me (once I explicitly set UseWPF). That fix may actually help you as well. It appears _GeneratedCodeFiles is a variable used by the WinFX (i.e. WPF) tasks.

from nullable.

manuelroemer avatar manuelroemer commented on May 18, 2024

That sounds amazing! I’ll close this as resolved then and make a mental note to document this in the README so that it is immediately clear to WPF users!

from nullable.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.