lundi 4 décembre 2023

C#: how to check if a property type is string? (nullable string) or string [duplicate]

giving this code:

var t = typeof(Sample);
var p1 = t.GetProperty(nameof(Sample.Str1));
var p2 = t.GetProperty(nameof(Sample.Str2));

Console.WriteLine(p1!.PropertyType == p2!.PropertyType);

public class Sample
{
    public string Str1 { get; set; } = default!;
    public string? Str2 { get; set; }
}

in a dotnet project with <Nullable>enable</Nullable> in csproj it prints "True"

but using int instead of string it shows "False".

Is there any way to know if the propertyType is the source code is "string" or "string?" ?





Aucun commentaire:

Enregistrer un commentaire