Click here to Skip to main content
15,895,829 members
Articles / Programming Languages / C# 4.0

The List Trifecta, Part 2

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
7 Sep 2013LGPL310 min read 28.7K   317   12  
The BDictionary is like a Dictionary mashed up with List<T>. BList and BMultiMap also say hello.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Loyc
{
	/// <summary>.NET Framework reflection doesn't offer complete type names for 
	/// generic types such as "List&lt;int>" (the Type.Name value of that class is 
	/// "List`1"). <see cref="GetGenericName"/> fills in the gap, and also saves the 
	/// computed name for fast repeated lookups.</summary>
	public static class MemoizedTypeName
	{
		static Dictionary<Type, string> _shortNames = new Dictionary<Type, string>();

		/// <summary>Computes a short language-agnostic name for a type, including 
		/// generic parameters, e.g. GenericName(typeof(int)) is "Int32"; 
		/// GenericName(typeof(Dictionary&lt;int, string>)) is 
		/// "Dictionary&lt;Int32, String>".</summary>
		/// <param name="type">Type whose name you want</param>
		/// <returns>Name with generic parameters, as explained in the summary.</returns>
		/// <remarks>The result is memoized for generic types, so that the name is
		/// computed only once.</remarks>
		public static string GetGenericName(Type type)
		{
			if (type == null)
				return null;
			string name;
			lock (_shortNames)
			{
				if (!_shortNames.TryGetValue(type, out name))
				{
					if (type.IsGenericType)
						_shortNames[type] = name = ComputeGenericName(type);
					else
						name = type.Name;
				}
			}
			return name;
		}

		/// <summary>Computes a type's name without memoization.</summary>
		internal static string ComputeGenericName(Type type)
		{
			string result = type.Name;
			if (type.IsGenericType)
			{
				// remove genric indication (e.g. `1)
				result = result.Substring(0, result.LastIndexOf('`'));

				result = string.Format(
					"{0}<{1}>",
					result,
					string.Join(", ", type.GetGenericArguments()
									  .Select(t => GetGenericName(t)).ToArray()));
			}
			return result;
		}

		/// <summary>Extension method on Type that is an alias for the <see cref="ShortName"/> method.</summary>
		public static string NameWithGenericParams(this Type t)
		{
			return GetGenericName(t);
		}
	}

	public static class MemoizedTypeName<T>
	{
		static string _name;
		public static string GenericName()
		{
			if (_name == null)
				_name = MemoizedTypeName.GetGenericName(typeof(T));
			return _name;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer None
Canada Canada
Since I started programming when I was 11, I wrote the SNES emulator "SNEqr", the FastNav mapping component, the Enhanced C# programming language (in progress), the parser generator LLLPG, and LES, a syntax to help you start building programming languages, DSLs or build systems.

My overall focus is on the Language of your choice (Loyc) initiative, which is about investigating ways to improve interoperability between programming languages and putting more power in the hands of developers. I'm also seeking employment.

Comments and Discussions