Skip to content

Instantly share code, notes, and snippets.

@tillig
Created January 4, 2017 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tillig/9539ec9ef6078e65243f776d910d9535 to your computer and use it in GitHub Desktop.
Save tillig/9539ec9ef6078e65243f776d910d9535 to your computer and use it in GitHub Desktop.
CodeSmith template for generating a KeyedCollection
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="Generates a strongly typed collection based on a generic KeyedCollection." %>
<%@ Property Name="Accessibility" Type="System.String" Optional="True" Default="public" Category="Options" Description="The accessibility of the collection class." %>
<%@ Property Name="ClassName" Type="System.String" Optional="True" Category="Context" Description="The name of the collection class." %>
<%@ Property Name="TargetNamespace" Type="System.String" Optional="True" Category="Namespaces" Description="The namespace of the collection class." %>
<%@ Property Name="KeyPropertyName" Type="System.String" Category="Context" Description="The name of the property on each collection member that returns the key value." %>
<%@ Property Name="KeyType" Type="System.String" Category="Context" Description="The key type of the collection." %>
<%@ Property Name="KeyNamespace" Type="System.String" Optional="True" Category="Namespaces" Description="The namespace of the key type." %>
<%@ Property Name="ItemType" Type="System.String" Category="Context" Description="The element type of the collection." %>
<%@ Property Name="ItemNamespace" Type="System.String" Optional="True" Category="Namespaces" Description="The namespace of the element type." %>
<%
System.Collections.Generic.List<String> namespaces = new System.Collections.Generic.List<String>();
namespaces.Add("System");
namespaces.Add("System.Collections.Generic");
namespaces.Add("System.Collections.ObjectModel");
if(!String.IsNullOrEmpty(KeyNamespace) && KeyNamespace != TargetNamespace && !namespaces.Contains(KeyNamespace))
{
namespaces.Add(KeyNamespace);
}
if(!String.IsNullOrEmpty(ItemNamespace) && ItemNamespace != TargetNamespace && !namespaces.Contains(ItemNamespace))
{
namespaces.Add(ItemNamespace);
}
foreach(string ns in namespaces)
{
%>
using <%=ns%>;
<%
}
%>
namespace <%=TargetNamespace %>
{
/// <summary>
/// A strongly typed <see cref="System.Collections.ObjectModel.KeyedCollection{T, T}" />
/// where each element is a <see cref="<%=ItemNamespace%>.<%=ItemType%>" /> and is keyed
/// off of the <see cref="<%=ItemNamespace%>.<%=ItemType%>.<%=KeyPropertyName%>" />
/// property.
/// </summary>
/// <seealso cref="<%=ItemNamespace%>.<%=ItemType%>" />
<%=Accessibility%> class <%=ClassName%> : KeyedCollection<<%=KeyType%>, <%=ItemType%>>
{
/// <summary>
/// Initializes a new instance of the <see cref="<%=TargetNamespace%>.<%=ClassName%>" /> class that uses the default equality comparer.
/// </summary>
/// <seealso cref="<%=TargetNamespace%>.<%=ClassName%>" />
public <%=ClassName%>() : base() { }
/// <summary>
/// Initializes a new instance of the <see cref="<%=TargetNamespace%>.<%=ClassName%>" /> class that uses the specified equality comparer.
/// </summary>
/// <param name="comparer">
/// The implementation of the <see cref="System.Collections.Generic.IEqualityComparer{T}" />
/// generic interface to use when comparing keys, or <see langword="null" /> to use the default
/// equality comparer for the type of the key, obtained from <see cref="System.Collections.Generic.EqualityComparer{T}.Default" />.
/// </param>
/// <seealso cref="<%=TargetNamespace%>.<%=ClassName%>" />
public <%=ClassName%>(IEqualityComparer<<%=KeyType%>> comparer) : base(comparer) { }
/// <summary>
/// Initializes a new instance of the <see cref="<%=TargetNamespace%>.<%=ClassName%>" /> class that uses the specified equality comparer.
/// </summary>
/// <param name="comparer">
/// The implementation of the <see cref="System.Collections.Generic.IEqualityComparer{T}" />
/// generic interface to use when comparing keys, or <see langword="null" /> to use the default
/// equality comparer for the type of the key, obtained from <see cref="System.Collections.Generic.EqualityComparer{T}.Default" />.
/// </param>
/// <param name="dictionaryCreationThreshold">
/// The number of elements the collection can hold without creating a lookup dictionary
/// (0 creates the lookup dictionary when the first item is added), or ?1 to specify that
/// a lookup dictionary is never created.
/// </param>
/// <seealso cref="<%=TargetNamespace%>.<%=ClassName%>" />
public <%=ClassName%>(IEqualityComparer<<%=KeyType%>> comparer, int dictionaryCreationThreshold) : base(comparer, dictionaryCreationThreshold) { }
/// <summary>
/// Extracts the key from the specified element.
/// </summary>
/// <param name="item">
/// The <see cref="<%=ItemNamespace%>.<%=ItemType%>" /> element from which to extract the key (<see cref="<%=ItemNamespace%>.<%=ItemType%>.<%=KeyPropertyName%>" />).
/// </param>
/// <seealso cref="<%=TargetNamespace%>.<%=ClassName%>" />
protected override <%=KeyType%> GetKeyForItem(<%=ItemType%> item)
{
return item.<%=KeyPropertyName%>;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment