manuelroemer / Nullable

A source code only package which allows you to use .NET's nullable attributes in older target frameworks like .NET Standard 2.0 or the "old" .NET Framework.
https://www.nuget.org/packages/Nullable
MIT License
186 stars 8 forks source link

Reference problem in WPF environment #20

Closed JakobFerdinand closed 3 years ago

JakobFerdinand commented 3 years ago

When using the library in a WPF environment it´s not possible to add a reference to the included types. I found that problem with my own library and was hoping, that you solved it somehow but I can recreate the issue with your project.

I created a sample project to recreate the issue. The branch nullable shows the problem with your library.

Its a simple console application that also includes a WPF User Control. image

using System;
using System.Diagnostics.CodeAnalysis;

namespace TheWPFClient
{
    class Program
    {
        [AllowNull] private readonly string OptionalValue;

        static void Main(string[] args)
        {
            Console.WriteLine("Hello beatiful World!");
        }
    }
}

When compiling the code that build error accours:

Program.cs(8,10,8,19): error CS0246: The type or namespace name 'AllowNullAttribute' could not be found (are you missing a using directive or an assembly reference?)
Program.cs(8,10,8,19): error CS0246: The type or namespace name 'AllowNull' could not be found (are you missing a using directive or an assembly reference?)

If you delete the usercontrol the error is gone.

chucker commented 3 years ago

This may be related to #11

JakobFerdinand commented 3 years ago

Oh thank you! Following you discussion to microsoft/CsWin32#7 guided me to the solution.

It´t the same problem with the temp project files during build and I have to add the following property in my project:

<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>
manuelroemer commented 3 years ago

Glad that this resolved your issue! I decided to pin #11 so that it is easier to discover in the future.

I also updated the README with a reference to the issue and with the solution for WPF users. Thanks @JakobFerdinand for raising the issue, that reminded me that I should update the README with this info.