Menu

#6 ParticipantCollection.GetHashCode() - deep algorithm

open
nobody
5
2009-01-06
2009-01-06
Anonymous
No

Submitted by: bluesbreaker@gmail.com

The ParticipantCollection's GetHashCode() doesn't get updated as participants join and leave. This is (IMO) a bug in the .NET collections, as Hashtable and IDictionary exhibit the same behavior. I understand that handling OnParticipantJoin and OnParticipantLeave are preferred ways to handle changes in the ParticipantCollection, however; for stateless applications, its handy to store a reference the hashCode and simply compare it later to see if anything has changed.

Here's a sample implementation that should work for any ICollection implementation (sorry the paste looks so horrible):

private Int32 GetDeepHash(Object obj)
{
StringBuilder hashStr = new StringBuilder();
hashStr.Append(obj.GetType().Name + " [" + obj.ToString() + "] ");
if (obj is IEnumerable)
{
hashStr.Append("{");
IEnumerable enumObj = (obj as IEnumerable);
foreach (Object childObj in enumObj)
{
hashStr.Append(GetDeepHash(childObj) + ",");
}
hashStr.Append("}");
}
return hashStr.ToString().GetHashCode();
}

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.