Marc,
Well, strictly speaking, Nullable<DateTi meis the correct name,
give-or-take some namespaces.
It returns incorrect name:
Nullable`1<Date Time>
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.GetUnd erlyingType(Typ e):
Type nullableType = Nullable.GetUnd erlyingType(typ e);
if (nullableType != null) return nullableType.Na me + "?";
Thank you. I created method below. Initially
TypeName( typeof(System.C ollections.Gene ric.List<string >) )
returns invalid type name which causes compile error :
System.Collecti ons.Generic.Lis t`1[[System.String, mscorlib, Version=2.0.0.0 ,
Culture=neutral , PublicKeyToken= b77a5c561934e08 9]]<System.Strin g>
To force it to return
System.Collecti ons.Generic.Lis t<System.String >
I looked all type properties using debugger but havent found any property
which returns legal type name System.Collecti ons.Generic.Lis t .
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">Typ e. May be generic or nullable</param>
/// <returns>Full type name, fully qualified namespaces</returns>
public static string TypeName(Type type)
{
Type nullableType = Nullable.GetUnd erlyingType(typ e);
if (nullableType != null)
return nullableType.Fu llName + "?";
if (!type.IsGeneri cType)
return type.FullName;
StringBuilder sb = new StringBuilder(
type.FullName.S ubstring(0, type.FullName.I ndexOf('`'))
);
sb.Append('<');
bool first = true;
foreach (Type t in type.GetGeneric Arguments())
{
if (!first)
{
sb.Append(',');
first = false;
}
sb.Append(TypeN ame(t));
}
sb.Append('>');
return sb.ToString();
}