Get PropertyType.Name in reflection from Nullable type

前端 未结 2 1792
悲&欢浪女
悲&欢浪女 2020-12-30 09:45

I want use reflection for get properties type. this is my code

var properties = type.GetProperties();
foreach (var propertyInfo in properties)
{
     model.         


        
2条回答
  •  無奈伤痛
    2020-12-30 10:26

    Change your code to look for nullable type, in that case take PropertyType as the first generic argument:

    var propertyType = propertyInfo.PropertyType;
    
    if (propertyType.IsGenericType &&
            propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
        {
          propertyType = propertyType.GetGenericArguments()[0];
        }
    
    model.ModelProperties.Add(new KeyValuePair
                            (propertyType.Name,propertyInfo.Name));
    

提交回复
热议问题