473,791 Members | 2,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collect ions.Generic.Li st<int>"

TypeName(typeof ( MyType<Customer >)) should return
"MyNamesp.MyTyp e<MyBusiness.Cu stomer>"

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 2414
You will need to use Type.GetGeneric Arguments and build up your own
string.
Jun 27 '08 #2
You will need to use Type.GetGeneric Arguments 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.IsGeneri cType)
return type.Name;

StringBuilder sb = new StringBuilder(t ype.Name);
sb.Append('<');
bool first = false;
foreach (Type t in type.GetGeneric Arguments())
{
if (first)
{
sb.Append(',');
first = false;
}
sb.Append(TypeN ame(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<Date Time>

How to force it to return correct nae:

DateTime?

Andrus.

Jun 27 '08 #5
Well, strictly speaking, Nullable<DateTi meis 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.GetUnd erlyingType(Typ e):

Type nullableType = Nullable.GetUnd erlyingType(typ e);
if (nullableType != null) return nullableType.Na me + "?";

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

Jun 27 '08 #7

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

Similar topics

2
32086
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 complex type: <s:complexContent><s:restriction base="SOAP-ENC:Array"><s:attribute ref="SOAP-ENC:arrayType" wsdl:arrayType="xsd:string"/></s:complexContent> Actual Output :
3
510
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 course, the generated proxy is not the same). When I am looking at the C++ generated code, it seems fine, but when I am executing the code, I always get the first choice type.
0
6473
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, 'b_id' => 9, 'c_id' => 3,
1
1392
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 .NET webservice. I am attempting to return an xml string similar to this: <?xml version="1.0" encoding="utf-8" ?>
12
3799
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. The Deserialization goes like: #Region " Load "
1
4423
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 intended for use, but I think this is the only solution for my situation. I searched the web, in the hope finding the answer, without any luck, so my final hope is with you. Let me explain the situation: I have an 'export'-wrapper to my regular...
18
2271
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 then want to use the string on the same page. My script is: function checkRadio(field) { for(var i=0; i < field.length; i++) {
1
2863
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 types and of more elaborate classes implementing IXmlSerializable. The resulting WSDL file for the webservice has two separate schemas in its <types> sections, and the client proxy (generated with wsdl.exe) is missing the definitions of the...
1
2758
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 testService() { ContactAddress cAddr = new ContactAddress(); cAddr.setAddresses1("AAAAAAAAAAAAA"); cAddr.setAddresses2("BBBBBBBBBBBBB"); cAddr.setAddresses3("CCCCCCCCCCCCC");
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.