简体   繁体   中英

Distinguish an explicitly nullable string type in C#

There are a lot of examples of string properties with "nullable" vs "non-nullable" declarations:

public MyClass {
    public string NotNullable {get;set;}
    public string? Nullable {get;set;}
}

This is even shown in the Microsoft C# official documentation on nullables as the primary example of nullable types.

This is made confusing by the fact that strings in C# can be null by default.

string myString = null; //100% valid, compile-able code

I have a script where I'm checking the types of properties in a class, to see if they are nullable. It's a long story why, but these nullable flags are put in a list and exported.

bool nullable = false;
if (classProperty.PropertyType.IsGenericType 
   && classProperty.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
    nullable = true;
}

This works for int? s, class references, DateTime? objects, basically anything other than string? , it can determine if it's an explicitly-declared nullable property or not.

Is there any way to determine if a property is string versus string? , and if not, why have (and laud in the documentation) a nullable string type for C#?


Also tried

An alternate method, comparing the PropertyType to a type derived directly:

modelProperty.PropertyType == typeof(int) //works
modelProperty.PropertyType == typeof(int?) //works
modelProperty.PropertyType == typeof(double) //works
modelProperty.PropertyType == typeof(double?) //works
modelProperty.PropertyType == typeof(string) //works
modelProperty.PropertyType == typeof(string?) //NOPE!

C# vomits an error about not being able to use the typeof operator on a nullable type when passed string? despite having literally just done so without issue for int? and double? . Also, modelProperty == typeof(string) is true whether the property was declared with either string or string? .

@Jesse got me pointed in the right direction. If anyone comes looking later, this is how I ended up checking and it seems to be working across the board:

if(modelProperty.PropertyType == typeof(string)
&& modelProperty.GetMethod?.CustomAttributes.Where(x => x.AttributeType.Name == "NullableContextAttribute").Count() == 0){
    nullable = true;
}

Still glad to entertain any more succinct answers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM