internal ExpandoClass FindNewClass(string newKey)
{
// just XOR the newKey hash code
int hashCode = _hashCode ^ newKey.GetHashCode();
lock (this)
{
List<WeakReference> infos = GetTransitionList(hashCode);
for (int i = 0; i < infos.Count; i++)
{
ExpandoClass klass = infos[i].Target as ExpandoClass;
if (klass == null)
{
infos.RemoveAt(i);
i--;
continue;
}
if (string.Equals(klass._keys[klass._keys.Length - 1], newKey, StringComparison.Ordinal))
{
// the new key is the key we added in this transition
return klass;
}
}
// no applicable transition, create a new one
string[] keys = new string[_keys.Length + 1];
Array.Copy(_keys, 0, keys, 0, _keys.Length);
keys[_keys.Length] = newKey;
ExpandoClass ec = new ExpandoClass(keys, hashCode);
infos.Add(new WeakReference(ec));
return ec;
}
}