ExtensionMethod.NET Home of 818 C#, Visual Basic, F# and Javascript extension methods

GetDisplayName()

Converts the pascal-cased Name property of a type to a displayable name.

Source

public static class CodeExpressionExtensions
{
    public static string GetDisplayName(this Type input)
    {
        string displayName = input.Name;

        for (int i = displayName.Length - 1; i >= 0; i--)
        {
            if (displayName[i] == char.ToUpper(displayName[i]))
                if (i > 0)
                    displayName = displayName.Insert(i, " ");
        }
        return displayName;
    }
}

Example

comboBox1.DisplayMember = "Name";
comboBox1.DataSource = typeof(CodeExpression).Assembly.GetTypes().Where(t => t.BaseType == typeof(CodeExpression)).Select(t => new { Name = t.GetDisplayName(), Type = t }).ToList();

Author: Jeroen de Zeeuw

Submitted on: 23 apr 2008

Language: C#

Type: System.Type

Views: 4902