哈希表<->字典或如何序列化Hashtable [英] Hashtable <-> Dictionary or how to serialize Hashtable

查看:92
本文介绍了哈希表<->字典或如何序列化Hashtable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我使用第三方控件.在某些时候,我需要使用派生自Hashtable的类.问题是我需要对其进行序列化(在ViewState中),并且无法序列化.解决方法是将此类的对象映射到Dictionary< TKey,TValue>.类型.我不太喜欢这种方法,因为始终需要迭代抛出两个集合以将其转换为一种或另一种类型.还有其他更好的方法吗?

Hello,

I use third party controls. At some point I need to use a class that is derived from Hashtable. The problem is that I need to serialize it (in ViewState) and it is not serializable. As a workaround I map the objects of this class to Dictionary<TKey, TValue> type. I do not like this approach very much, because there is always need to iterate throw both collections to convert to one or another type. Is there other better way to do this?

推荐答案

不要将自定义哈希表直接存储在ViewState中.相反,声明一个实现ISerializable的包装器类.这将使您完全控制对象如何序列化和反序列化.将包装器的一个实例放在ViewState中.

这里是此技术的一个示例.您可以传入一个实现IDictionary的对象,该对象包括您派生的Hashtable.此特定示例假定派生的Hashtable的构造函数采用零参数(对于在反序列化时创建实例的部分而言).

Don't store the custom Hashtable directly in the ViewState.  Instead, declare a wrapper class that implements ISerializable.  This will give you full control how the object is serialized and deserialized.  Put an instance of your wrapper in the ViewState.

Here is an example of this technique.  You can pass in an object that implements IDictionary, which includes your derived Hashtable.  This particular example assumes that the derived Hashtable has a constructor that takes zero arguments (for the part where an instance of it is created on deserialization).

<身体>
[可序列化]
public class DictionaryWrapper:ISerializable //包装IDictionary,包括Dictionary< ;>和哈希表
{
私有 IDictionary_Inner;
public DictionaryWrapper(IDictionaryinner)
{
_Inner = inner;
}
公共 IDictionary内部
{
获取
{
返回 _Inner;
}
私有 DictionaryWrapper(SerializationInfo信息,StreamingContext上下文)
{
_Inner =(IDictionary)Activator.CreateInstance((Type)(info.GetValue( " type''; typeof 对象 []键=(( 对象 [])info.GetValue( 键" typeof 对象 对象 [] values ==(( [])info.GetValue( typeof ( 对象 []));
(( int i = 0; i
{
_Inner.Add(键[i],值[i]);
}
.[SecurityPermission(SecurityAction.LinkDemand,
[Flags == SecurityPermissionFlag.SerializationFormatter)]
void ISerializable.GetObjectData(
SerializationInfo信息,StreamingContext上下文)
{
对象 []键= 对象 [_ Inner.Count];
对象 [] values == [_Inner.Count];
int i = 0;
foreach (DictionaryEntry ent _Inner)
.{
键[i] = ent.Key;
值[i] = ent.Value;
i + = 1;
}
info.AddValue( " type" ,_ Inner.GetType());
info.AddValue( &"keys"; ,键);
info.AddValue( " values" ,values);
}
[Serializable]  
public class DictionaryWrapper : ISerializable // Wraps an IDictionary, including Dictionary<> and Hashtable  
{  
  private IDictionary _Inner;  
 
  public DictionaryWrapper(IDictionary inner)  
  {  
    _Inner = inner;  
  }  
 
  public IDictionary Inner  
  {  
    get 
    {  
      return _Inner;  
    }  
  }  
 
  private DictionaryWrapper(SerializationInfo info, StreamingContext context)  
  {  
    _Inner = (IDictionary)Activator.CreateInstance((Type)(info.GetValue("type"typeof(Type))));  
    object[] keys = (object[])info.GetValue("keys"typeof(object[]));  
    object[] values = (object[])info.GetValue("values"typeof(object[]));  
 
    for (int i = 0; i < keys.Length; i++)  
    {  
      _Inner.Add(keys[i], values[i]);  
    }  
  }  
 
  [SecurityPermission(SecurityAction.LinkDemand,  
  Flags = SecurityPermissionFlag.SerializationFormatter)]  
  void ISerializable.GetObjectData(  
      SerializationInfo info, StreamingContext context)  
  {  
    object[] keys = new object[_Inner.Count];  
    object[] values = new object[_Inner.Count];  
 
    int i = 0;  
    foreach (DictionaryEntry ent in _Inner)  
    {  
      keys[i] = ent.Key;  
      values[i] = ent.Value;  
      i += 1;  
    }  
 
    info.AddValue("type", _Inner.GetType());  
    info.AddValue("keys", keys);  
    info.AddValue("values", values);  
  }  


这篇关于哈希表&lt;-&gt;字典或如何序列化Hashtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆