473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Return type name as string

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.

Jun 27 '08 #1
6 2394
You will need to use Type.GetGenericArguments and build up your own
string.
Jun 27 '08 #2
You will need to use Type.GetGenericArguments and build up your own
string.
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();
}

Jun 27 '08 #3
I think you mean 'if (!first)' and then you want to set it to true
inside that 'if' statement.
Jun 27 '08 #4
>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.

Jun 27 '08 #5
Well, strictly speaking, Nullable<DateTimeis the correct name,
give-or-take some namespaces. 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 + "?";

Marc
Jun 27 '08 #6
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();
}

Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: burdeen | last post by:
I've been trying to return an array with PHP5 soap. Is this not supported? or am i doing it wrong? Seems to return on the last element in the array. In my WSDL i've defined my return type as a...
3
by: dgaucher | last post by:
Hi, I want to consume a Web Service that returns a choice, but my C++ client always receives the same returned type. On the other hand, when I am using a Java client, it is working fine (of...
0
by: Marc van Boven | last post by:
I'm stuck with the following problem: My nusoap-client calls a server-function giveCombination(). The function giveCombination should return something like array( => array( 'a_id' => 6,...
1
by: Peter Nofelt | last post by:
Hey all, I want to return an xml structure without .net trying to inject any of its xml schema? Can this be done? Here is the scenario: I'm running into an issue with the return string of my...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
1
by: Thomas D. | last post by:
Hello all, I'm using the IXmlSerializable interface for a project and encounter some problems when testing my webservice in a client application. I know this interface is undocumented and not...
18
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
1
by: louis_la_brocante | last post by:
Dear all, I am having trouble generating a client proxy for a webservice whose methods return a "complex" type. The type is complex in that it is a class whose members are a mix of primitive...
1
by: v4u2chat | last post by:
Do I need to extend any of classes from AXIS to return multiple values? I'm exposing the following method as web service through AXIS to return multiple values. public ContactAddress...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.