Get PropertyType.Name in reflection from Nullable type

c#, nullable, reflection

Solution

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<Type, string>
                        (propertyType.Name,propertyInfo.Name));

Problem

I want use reflection for get properties type. this is my code ``` var properties = type.GetProperties(); foreach (var propertyInfo in properties) { model.ModelProperties.Add( new KeyValuePair<Type, string> (propertyInfo.PropertyType.Name, propertyInfo.Name) ); } ``` this code `propertyInfo.PropertyType.Name` is ok but if my property type is `Nullable` i get this `Nullable'1` string and if write `FullName` if get this stirng `System.Nullable1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]`

Original source

Related problems