Return type name as string

A

Andrus

I need to create method which returns my application object type as string :

TypeName(typeof( List<int>)) should return "List<int>" or
"System.Collections.Generic.List<int>"

TypeName(typeof( MyType<Customer>)) should return
"MyNamesp.MyType<MyBusiness.Customer>"

etc.


I tried

static string TypeName(Type type) {
return type.Name;
}

but this returns wrong names (´1 etc) for generic types.

How to get correct type name for generic types ?

Andrus.
 
N

not_a_commie

You will need to use Type.GetGenericArguments and build up your own
string.
 
A

Andrus

You will need to use Type.GetGenericArguments and build up your own

Thank you.
I created method below. For initial tests it seems to work OK.
Is this best solution ?

Andrus.

/// <returns>type name as string</returns>
static string TypeName(Type type)
{

if (!type.IsGenericType)
return type.Name;

StringBuilder sb = new StringBuilder(type.Name);
sb.Append('<');
bool first = false;
foreach (Type t in type.GetGenericArguments())
{
if (first)
{
sb.Append(',');
first = false;
}
sb.Append(TypeName(t));
}
sb.Append('>');
return sb.ToString();
}
 
N

not_a_commie

I think you mean 'if (!first)' and then you want to set it to true
inside that 'if' statement.
 
A

Andrus

I think you mean 'if (!first)' and then you want to set it to true
inside that 'if' statement.

for type

DateTime?

this function returns invalid type name:

Nullable'1<DateTime>

How to force it to return correct nae:

DateTime?

Andrus.
 
M

Marc Gravell

Well, strictly speaking, Nullable<DateTime> is the correct name,
give-or-take some namespaces. DateTime? is only C# specific. But you
could do this by checking for Nullable<T> as 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 + "?";

Marc
 
A

Andrus

Marc,
Well, strictly speaking, Nullable<DateTime> is the correct name,
give-or-take some namespaces.

It returns incorrect name:

Nullable`1 said:
DateTime? is only C# specific. But you could do this by checking for
Nullable<T> as 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();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top