Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9116809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:45:44+00:00 2026-06-17T04:45:44+00:00

I can’t believe that this hasn’t been asked before but as I am unable

  • 0

I can’t believe that this hasn’t been asked before but as I am unable to find this particular “needle” in the stackoverflow haystack I’m asking it.

I would like a neater implementation of GetType().ToString() for closed generic types. By neater I mean that I would like the ToString() call to return a string in the format I would have typed into the development environment.

The default GetType().ToString() on a closed generic will place `n where n is the number of types, and uses [ ]’s instead of <>’s.

Anyway … to the code.

given the following classes:

A a = new A();
C<A> c = new C<A>();
D<A, B> d = new D<A, B>();
E<B, C<A>, D<A, B>> e = new E<B, C<A>, D<A, B>>();

The default .ToString() returns:

AAA.Events.A
AAA.Events.C`1[AAA.Events.A]
AAA.Events.D`2[AAA.Events.A,AAA.Events.B]
AAA.Events.E`3[AAA.Events.B,AAA.Events.C`1[AAA.Events.A],AAA.Events.D`2

I’ve written some basic old school (pre linq) code to produce:

AAA.Events.A
AAA.Events.C<AAA.Events.A>
AAA.Events.D<AAA.Events.A,AAA.Events.B>
AAA.Events.E<AAA.Events.B,AAA.Events.C<AAA.Events.A>,AAA.Events.D<AAA.Events.A,AAA.Events.B>>

But I would really like it to return:

A
C<A>
D<A,B>
E<B,C<A>,D<A,B>>

Here’s the shoddy code I have written to demonstrate my intent:

public static string NeaterString(this Type t)
{
    string neater = t.ToString();
    if (neater.Contains('`'))
    {
        while (neater.Contains('`'))
        {
            neater = neater.Remove(neater.IndexOf('`'), 2);
        }
        return neater.Replace('[', '<').Replace(']', '>');
    }
    else return neater; 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:45:45+00:00Added an answer on June 17, 2026 at 4:45 am

    I would try to avoid as much string parsing as possible. My routine looks like this:

    public static string GetFriendlyName(Type type)
    {
        if (type.IsGenericType)
            return string.Format("{0}<{1}>", type.Name.Split('`')[0], string.Join(", ", type.GetGenericArguments().Select(x => GetFriendlyName(x))));
        else
            return type.Name;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Can find why i get this error can someone help? package Android.data; public class
Can anyone help me trying to find out why this doesn't work. The brushes
Can anyone explain to me why this program: for(float i = -1; i <
I have a jquery bug and I've been looking for hours now, I can't
Can someone help me understand this question? I may have it on my tomorrow
This could be a duplicate question, but I have no idea what search terms
can someone explain what means this line, or provide with source where it is
Can someone please explain how this code is working for me? I don't understand
Can I write this with less code, also is .click(function preferred for mobile or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.