Skip to content

Blazor WASM does not handle NullabilityInfoContext correctly #7171

Open
@juergenr

Description

@juergenr

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The NullabilityInfoContext behaves different in Blazor and a Console application.

The results of the NullabilityInfoContext are wrong in Blazor and correct in a Console applcation for Property-Types like:
public string[]? StringB { get; set; }
public string?[] StringC { get; set; }
public object[]? ObjectB { get; set; }
public object?[] ObjectC { get; set; }

Expected Behavior

Same results in Blazor and a Console application.

Steps To Reproduce

@code {
    public class Test {
        public string[]   StringA { get; set; } = new string[]  { "string1", "string2" };
        public string[]?  StringB { get; set; } = new string[]  { "string1", "string2" };
        public string?[]  StringC { get; set; } = new string?[] { "string1", "string2" };
        public string?[]? StringD { get; set; } = new string?[] { "string1", "string2" };

        public double[]   DoubleA { get; set; } = new double[]  { 1, 2 };
        public double[]?  DoubleB { get; set; } = new double[]  { 1, 2 };
        public double?[]  DoubleC { get; set; } = new double?[] { 1, 2 };
        public double?[]? DoubleD { get; set; } = new double?[] { 1, 2 };

        public object[]   ObjectA { get; set; } = new object[]  { 1, 2 };
        public object[]?  ObjectB { get; set; } = new object[]  { 1, 2 };
        public object?[]  ObjectC { get; set; } = new object?[] { 1, 2 };
        public object?[]? ObjectD { get; set; } = new object?[] { 1, 2 };
    }
    Test test = new Test();

    protected override Task OnInitializedAsync() {
        NullabilityInfoContext nullabilityInfoContext = new();

        PropertyInfo[] piArray = typeof(Test).GetProperties();
        foreach (PropertyInfo pi in piArray) {
            NullabilityInfo ni = nullabilityInfoContext.Create(pi);
            Console.WriteLine($"Name: {pi.Name}  ArrayState: {ni.ReadState}  ElementState: {ni.ElementType.ReadState}");
        }        
        return base.OnInitializedAsync();
    }
}

Output from Blazor is:

Name: StringA  ArrayState: NotNull  ElementState: NotNull   
Name: StringB  ArrayState: NotNull  ElementState: NotNull     <= Error 
Name: StringC  ArrayState: NotNull  ElementState: NotNull     <= Error
Name: StringD  ArrayState: Nullable  ElementState: Nullable
Name: DoubleA  ArrayState: NotNull  ElementState: NotNull
Name: DoubleB  ArrayState: Nullable  ElementState: NotNull
Name: DoubleC  ArrayState: NotNull  ElementState: Nullable
Name: DoubleD  ArrayState: Nullable  ElementState: Nullable
Name: ObjectA  ArrayState: NotNull  ElementState: NotNull
Name: ObjectB  ArrayState: NotNull  ElementState: NotNull    <= Error
Name: ObjectC  ArrayState: NotNull  ElementState: NotNull    <= Error
Name: ObjectD  ArrayState: Nullable  ElementState: Nullable

Output from same code executed in a Console Application:

Name: StringA  ArrayState: NotNull  ElementState: NotNull   
Name: StringB  ArrayState: Nullable  ElementState: NotNull     <= Correct
Name: StringC  ArrayState: NotNull  ElementState: Nullable     <= Correct
Name: StringD  ArrayState: Nullable  ElementState: Nullable
Name: DoubleA  ArrayState: NotNull  ElementState: NotNull
Name: DoubleB  ArrayState: Nullable  ElementState: NotNull
Name: DoubleC  ArrayState: NotNull  ElementState: Nullable
Name: DoubleD  ArrayState: Nullable  ElementState: Nullable
Name: ObjectA  ArrayState: NotNull  ElementState: NotNull
Name: ObjectB  ArrayState: Nullable  ElementState: NotNull    <= Correct
Name: ObjectC  ArrayState: NotNull  ElementState: Nullable    <= Correct
Name: ObjectD  ArrayState: Nullable  ElementState: Nullable

Exceptions (if any)

No response

.NET Version

6.0.400-preview.22301.10

Anything else?

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions