Live Templates::Declare Collection Class

I'm using the following template to quickly create typed collections.

May be somebody will find this interesting...
Let me know anyway... ;)

$COL_NAME$ => Editable field #1
$COL_TYPE$ => Editable field #2

/// A collection of  objects.
/// 
[Serializable]
public class $COL_NAME$: CollectionBase
{
	public $COL_NAME$() {}

	public int Add($COL_TYPE$ value)
	{
		return InnerList.Add(value);
	}

	public void Insert(int index, $COL_TYPE$ value)
	{
		InnerList.Insert(index, value);
	}

	public void Remove($COL_TYPE$ value)
	{
		InnerList.Remove(value);
	}

	public int IndexOf($COL_TYPE$ value)
	{
		return InnerList.IndexOf(value);
	}

	public bool Contains($COL_TYPE$ value)
	{
		return InnerList.Contains(value);
	}

	public $COL_TYPE$ this[int index]
	{
		get {return ($COL_TYPE$)InnerList[index];}
		set {InnerList[index] = value;}
	}

	protected override void OnValidate(object value)
	{
		base.OnValidate(value);
		if (!(value is $COL_TYPE$))
			throw new ApplicationException("Invalid collection element type.");
	}
}
]]>

2 comments
Comment actions Permalink

Hello Chepel,

C> I'm using the following template to quickly create typed collections.
By using InnerList you breaks ability of CollectionBase to notify inheritors
about changes, via OnRemoveComplete methods and such. You should use List
property, or make class sealed.



0
Comment actions Permalink

You're right, sealed is a good idea in this case...

0

Please sign in to leave a comment.