Skip to content

Instantly share code, notes, and snippets.

@MatthewKing
Created July 22, 2012 12:49
Show Gist options
  • Save MatthewKing/3159561 to your computer and use it in GitHub Desktop.
Save MatthewKing/3159561 to your computer and use it in GitHub Desktop.
Extension method to get a user-friendly string representation of a specified Type's name.
// Copyright Matthew King 2012.
// Boost Software License - Version 1.0 - August 17th, 2003
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Linq;
/// <summary>
/// Extension methods for System.Type.
/// </summary>
public static class TypeExtensions
{
/// <summary>
/// Returns a human-friendly representation of the Type's name.
/// </summary>
/// <param name="type">The Type.</param>
/// <returns>A human-friendly representation of the Type's name.</returns>
public static string GetFriendlyName(this Type type)
{
if (type == null)
throw new ArgumentNullException("type", "type should not be null.");
if (!type.IsGenericType || type.IsGenericParameter)
return type.Name;
const StringComparison sc = StringComparison.Ordinal;
string name = type.Name.Substring(0, type.Name.IndexOf("`", sc));
string[] arguments = type.GetGenericArguments()
.Select(arg => arg.GetFriendlyName())
.ToArray();
return String.Concat(name, '<', String.Join(", ", arguments), '>');
}
}
// Copyright Matthew King 2012.
// Boost Software License - Version 1.0 - August 17th, 2003
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using NUnit.Framework;
[TestFixture]
public sealed class TypeExtensionsTests
{
[Test]
public void GetFriendlyName_TypeIsNull_ThrowsArgumentNullException()
{
Type type = null;
const string message = "type should not be null.";
Assert.That(() => type.GetFriendlyName(),
Throws.TypeOf<ArgumentNullException>()
.And.Message.Contains(message));
}
[TestCaseSource("GetFriendlyName_TestCaseData")]
public string GetFriendlyName(Type type)
{
return type.GetFriendlyName();
}
private static IEnumerable<TestCaseData> GetFriendlyName_TestCaseData()
{
yield return new TestCaseData(typeof(string))
.Returns("String");
yield return new TestCaseData(typeof(String))
.Returns("String");
yield return new TestCaseData(typeof(DateTime))
.Returns("DateTime");
yield return new TestCaseData(typeof(List<String>))
.Returns("List<String>");
yield return new TestCaseData(typeof(Nullable<DateTime>))
.Returns("Nullable<DateTime>");
yield return new TestCaseData(typeof(IList<IEnumerable<IDictionary<String, Int32>>>))
.Returns("IList<IEnumerable<IDictionary<String, Int32>>>");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment