public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException(nameof(info));
}
Contract.EndContractBlock();
// This is imperfect - it only works well if all other writes are
// also using our synchronized wrapper. But it's still a good idea.
lock (SyncRoot)
{
// This method hasn't been fully tweaked to be safe for a concurrent writer.
int oldVersion = _version;
info.AddValue(LoadFactorName, _loadFactor);
info.AddValue(VersionName, _version);
//
// We need to maintain serialization compatibility with Everett and RTM.
// If the comparer is null or a compatible comparer, serialize Hashtable
// in a format that can be deserialized on Everett and RTM.
//
// Also, if the Hashtable is using randomized hashing, serialize the old
// view of the _keycomparer so perevious frameworks don't see the new types
#pragma warning disable 618
IEqualityComparer keyComparerForSerilization = _keycomparer;
if (keyComparerForSerilization == null)
{
info.AddValue(ComparerName, null, typeof(IComparer));
info.AddValue(HashCodeProviderName, null, typeof(IHashCodeProvider));
}
else if (keyComparerForSerilization is CompatibleComparer)
{
CompatibleComparer c = keyComparerForSerilization as CompatibleComparer;
info.AddValue(ComparerName, c.Comparer, typeof(IComparer));
info.AddValue(HashCodeProviderName, c.HashCodeProvider, typeof(IHashCodeProvider));
}
else
{
info.AddValue(KeyComparerName, keyComparerForSerilization, typeof(IEqualityComparer));
}
#pragma warning restore 618
info.AddValue(HashSizeName, _buckets.Length); //This is the length of the bucket array.
Object[] serKeys = new Object[_count];
Object[] serValues = new Object[_count];
CopyKeys(serKeys, 0);
CopyValues(serValues, 0);
info.AddValue(KeysName, serKeys, typeof(Object[]));
info.AddValue(ValuesName, serValues, typeof(Object[]));
// Explicitly check to see if anyone changed the Hashtable while we
// were serializing it. That's a race in their code.
if (_version != oldVersion)
{
throw new InvalidOperationException(SR.InvalidOperation_EnumFailedVersion);
}
}
}