17

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]]

Venson
  • 1,772
  • 17
  • 37
  • Is it a Nullable? – It'sNotALie. Feb 16 '13 at 13:05
  • And which is the string that you want to get? It looks like you will have to use the properties/methods on PropertyType that allows you to access the generic parameters of the type. – Oskar Berggren Feb 16 '13 at 13:06
  • 2
    http://stackoverflow.com/questions/5174423/getting-basic-datatype-rather-than-weird-nullable-one-via-reflection-in-c-sha – TheNextman Feb 16 '13 at 13:07
  • 2
    See [this answer](http://stackoverflow.com/a/401824/1174942). It creates a readable name for both generic and non-generic types (`System.Nullable` for example). – Mohammad Dehghan Feb 16 '13 at 13:16

2 Answers2

38

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));
Kilhoffer
  • 32,375
  • 22
  • 97
  • 124
Igoy
  • 2,942
  • 22
  • 23
12

This is an old question, but I ran into this as well. I like @Igoy's answer, but it doesn't work if the type is an array of a nullable type. This is my extension method to handle any combination of nullable/generic and array. Hopefully it will be useful to someone with the same question.

public static string GetDisplayName(this Type t)
{
    if(t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))
        return string.Format("{0}?", GetDisplayName(t.GetGenericArguments()[0]));
    if(t.IsGenericType)
        return string.Format("{0}<{1}>",
                             t.Name.Remove(t.Name.IndexOf('`')), 
                             string.Join(",",t.GetGenericArguments().Select(at => at.GetDisplayName())));
    if(t.IsArray)
        return string.Format("{0}[{1}]", 
                             GetDisplayName(t.GetElementType()),
                             new string(',', t.GetArrayRank()-1));
    return t.Name;
}

This will handle cases as complicated as this:

typeof(Dictionary<int[,,],bool?[][]>).GetDisplayName()

Returns:

Dictionary<Int32[,,],Boolean?[][]>
Joe Skeen
  • 1,727
  • 16
  • 18