本文介绍了哈希表<->字典或如何序列化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?
推荐答案
[可序列化] |
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 (Type))))); |
对象 []键=(( 对象 [])info.GetValue( 键" , typeof ( 对象 [])); |
对象 [] values ==(( 对象 [])info.GetValue( " values" , 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); |
} |
} |
这篇关于哈希表<->字典或如何序列化Hashtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!