admin管理员组

文章数量:1399953

I have the following function:

protected bool IsNullableParam(string name, Type type)
{
    bool isNullable;

    Type? realType = Nullable.GetUnderlyingType(type);
    if (realType != null) 
    {
       isNullable = false;
    }
    else 
    {
       isNullable = true;
    }

    return isNullable;
}

...
...
string? s = "test";
ParameterSchema("name", s.GetType());
....

When I call it like in the example isNullable will be set to false. I know from other posts that GetUnderlyingType doesn't work to check nullability of a string and I've seen a few solutions in case of PropertyInfo and FieldInfo but I couldn't find a solution when Type is used. In my case I have only a Type instance. Is there a workaround to determine via reflection if a string is nullable ?

本文标签: netCheck if a string is nullable from Type in CStack Overflow