最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.net - Check if a string is nullable from Type in C# - Stack Overflow

programmeradmin5浏览0评论

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 ?

发布评论

评论列表(0)

  1. 暂无评论