Marc,
Well, strictly speaking, Nullable<DateTimeis the correct name,
give-or-take some namespaces.
It returns incorrect name:
Nullable`1<DateTime>
DateTime? is only C# specific. But you could do this by checking for
Nullable<Tas a special case. There is a helper method as it happens;
Nullable.GetUnderlyingType(Type):
Type nullableType = Nullable.GetUnderlyingType(type);
if (nullableType != null) return nullableType.Name + "?";
Thank you. I created method below. Initially
TypeName( typeof(System.Collections.Generic.List<string>) )
returns invalid type name which causes compile error :
System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089]]<System.String>
To force it to return
System.Collections.Generic.List<System.String>
I looked all type properties using debugger but havent found any property
which returns legal type name System.Collections.Generic.List .
So I take leftmost characters from FullName up to ` character .
Is this best solution ?
Andrus.
/// <summary>
/// Get full type name with full namespace names
/// </summary>
/// <param name="type">Type. May be generic or nullable</param>
/// <returns>Full type name, fully qualified namespaces</returns>
public static string TypeName(Type type)
{
Type nullableType = Nullable.GetUnderlyingType(type);
if (nullableType != null)
return nullableType.FullName + "?";
if (!type.IsGenericType)
return type.FullName;
StringBuilder sb = new StringBuilder(
type.FullName.Substring(0, type.FullName.IndexOf('`'))
);
sb.Append('<');
bool first = true;
foreach (Type t in type.GetGenericArguments())
{
if (!first)
{
sb.Append(',');
first = false;
}
sb.Append(TypeName(t));
}
sb.Append('>');
return sb.ToString();
}