Determine if a reference type is nullable at runtime
Asked Answered
C

1

5

In C# it's trivial to determine whether a value type is nullable. Specifically, you can obtain this information from the Type itself; for example:

public static bool IsNullable(this Type type) =>
    type.IsValueType && Nullable.GetUnderlyingType(type) is not null;

As I understand, it's possible to obtain nullability information for nullable reference types via NullableAttribute or NullableContextAttribute, but it would appear that this has to be obtained from MemberInfo, rather than from the Type.

Is this understanding correct, or is there a workaround to obtain nullability information for nullable reference types from a Type?

Chaparajos answered 9/9 at 12:41 Comment(5)
Duplicate? #58454472 The question is a bit unclear, a string? is not a different type than string, so there is no way to check if it's a nullable reference type. You can just check it if you have the MemberInfoKellby
a type, in the context of a class / reference-type, does not know whether it is even being used in a NRT-enabled context, let alone what the final NRT designation is; so: no, this is not possible from a Type. The MemberInfo: is more contextual, so: yupScornik
It is precisely because you cannot obtain nullability information from Type that NullableAttribute and NullableContextAttribute are introduced.Rutherford
You don't write public class Foo? { } or similar. You create a class Foo, and then variables / fields / parameters / etc have a type which is Foo or Foo?. Therefore, it is not the type itself which is nullable, but the variable / field / parameter which uses that type. Therefore, it makes sense that you can't get nullability information from a Type, because such a thing wouldn't make any senseShippee
It's not a duplicate. The other question asks, "Is there a way to check a class property uses a nullable reference type via reflection?", to which the answer is yes. The current question asks about doing it from a Type, which is not possible.Euglena
K
8

Nullable reference types (C# reference):

Nullable reference types aren't new class types, but rather annotations on existing reference types. The compiler uses those annotations to help you find potential null reference errors in your code. There's no runtime difference between a non-nullable reference type and a nullable reference type.

That's the reason why you can't use typeof on a nullable reference type:

Type t = typeof(string?); // error CS8639

So you need a PropertyInfo or FieldInfo if you want to check if it's nullable, for example:

public static bool IsMarkedAsNullable(PropertyInfo p)
{
    return new NullabilityInfoContext().Create(p).WriteState is NullabilityState.Nullable;
}

Copied extension from here: How to use .NET reflection to check for nullable reference type

Kellby answered 9/9 at 13:0 Comment(2)
I'd hazard a guess that given a Type instance, there is also no way to obtain the MemberInfo? that declared the type, without any other information than the Type itself?Chaparajos
@MatthewLayton: No, a System.Type has just information about the type itself, not about where it is used(it can be used in any context, sometimes nullable and sometimes not).Kellby

© 2022 - 2024 — McMap. All rights reserved.