10

I have an object (MyObject) with a property (MyProperty). I want to get it's type name (i.e. String or MyClass etc.). I use:

PropertyInfo propInfo = typeof(MyObject).GetProperty("MyProperty");
Console.WriteLine(propInfo.PropertyType.Name);
Console.WriteLine(propInfo.PropertyType.FullName);

No problem with simple types, but when MyProperty is a generic type, I face problems on getting it's name (e.g. Collection<String>). It prints:

Collection`1

System.Collections.ObjectModel.Collection`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

What is that `1? And how can I obtain "Collection<String>"?

1
  • 2
    Collection`1 means a generic collection with 1 generic type parameter
    – Leom Burke
    Jul 5, 2011 at 14:27

3 Answers 3

11

The `1 means a generic type, with 1 generic parameter.

One way of getting the string is by using the System.CodeDom, as suggested by @LukeH:

using System;
using System.CodeDom;
using System.Collections.Generic;
using Microsoft.CSharp;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var p = new CSharpCodeProvider())
            {
                var r = new CodeTypeReference(typeof(Dictionary<string, int>));
                
                Console.WriteLine(p.GetTypeOutput(r));
            }
        }
    }
}

An alternative method is here. See below for @jaredpar's code:

public static string GetFriendlyTypeName(Type type) {
    if (type.IsGenericParameter)
    {
        return type.Name;
    }

    if (!type.IsGenericType)
    {
        return type.FullName;
    }

    var builder = new System.Text.StringBuilder();
    var name = type.Name;
    var index = name.IndexOf("`");
    builder.AppendFormat("{0}.{1}", type.Namespace, name.Substring(0, index));
    builder.Append('<');
    var first = true;
    foreach (var arg in type.GetGenericArguments())
    {
        if (!first)
        {
            builder.Append(',');
        }
        builder.Append(GetFriendlyTypeName(arg));
        first = false;
    }
    builder.Append('>');
    return builder.ToString();
}
4
  • You might want to handle Nullables, and you might want to add a space after the comma.
    – SLaks
    Jul 5, 2011 at 14:37
  • If you copy the complete source, you might want to credit JaredPar with more than just a link to the question...
    – Heinzi
    Jul 5, 2011 at 14:37
  • type.GetGenericTypeDefinition().FullName
    – SLaks
    Jul 5, 2011 at 14:38
  • @SLaks, that gives the same (with `1). Jul 5, 2011 at 14:44
8

This is a CLR internal typename.

The number is the number of generic type parameters, since types can be overloaded.
(Func`1 and Func`2 are different types)

There is no built-in way to get a C#-style typename, since the CLR has nothing to do with C#.

3
  • 5
    You can actually get at the C#-style name using CodeDom, but I probably wouldn't bother! Something like using (var p = new CSharpCodeProvider()) { var r = new CodeTypeReference(propInfo.PropertyType); Console.WriteLine(p.GetTypeOutput(r)); }
    – LukeH
    Jul 5, 2011 at 14:40
  • @LukeH: Looking at the source, that only handles arrays, not generics.
    – SLaks
    Jul 5, 2011 at 14:42
  • 3
    @LukeH, i think that comment should be an answer. Jul 5, 2011 at 14:45
0

SLaks already explained what `1 means.

About your second question: You can obtain the name of the generic type parameters by using Type.GetGenericArguments:

if (propInfo.PropertyType.IsGenericType) {
    Type[] typeArguments = propInfo.PropertyType.GetGenericArguments();
    // typeArguments now contains an array of types ({String} in your example).
}
2
  • 1
    He wants a string, not an array of type parameters.
    – SLaks
    Jul 5, 2011 at 14:33
  • @SLaks: Good point. I'll leave that as an exercise to the reader. ;-)
    – Heinzi
    Jul 5, 2011 at 14:34

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.