IDictionaryConverter.cs
上传用户:szgaoree
上传日期:2009-01-05
资源大小:74k
文件大小:3k
源码类别:

Ajax

开发平台:

C#

  1. /*
  2.  * MS   05-12-21    added Deserialize for Hashtables
  3.  *                  JavaScript object will now include the type for key and value
  4.  * 
  5.  * 
  6.  * 
  7.  */
  8. using System;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Collections;
  12. using System.Collections.Specialized;
  13. namespace AjaxPro
  14. {
  15. /// <summary>
  16. /// Provides methods to serialize and deserialize an object that implements IDictionary.
  17. /// </summary>
  18. public class IDictionaryConverter : IJavaScriptConverter
  19. {
  20. public IDictionaryConverter() : base()
  21. {
  22. }
  23.         public override object Deserialize(IJavaScriptObject o, Type t)
  24.         {
  25.             if (!(o is JavaScriptArray))
  26.                 throw new NotSupportedException();
  27.             JavaScriptArray a = (JavaScriptArray)o;
  28.             for (int i = 0; i < a.Count; i++)
  29.                 if (!(a[i] is JavaScriptArray))
  30.                     throw new NotSupportedException();
  31.             IDictionary d = (IDictionary)Activator.CreateInstance(t);
  32.             object key;
  33.             object value;
  34.             JavaScriptArray aa;
  35.             for (int i = 0; i < a.Count; i++)
  36.             {
  37.                 aa = (JavaScriptArray)a[i];
  38.                 key = JavaScriptDeserializer.Deserialize((IJavaScriptObject)aa[0], Type.GetType(((JavaScriptString)aa[2]).ToString()));
  39.                 value = JavaScriptDeserializer.Deserialize((IJavaScriptObject)aa[1], Type.GetType(((JavaScriptString)aa[3]).ToString()));
  40.                 d.Add(key, value);
  41.             }
  42.             return d;
  43.         }
  44. public override string Serialize(object o)
  45. {
  46. if(!(o is IDictionary))
  47. throw new NotSupportedException();
  48. StringBuilder sb = new StringBuilder();
  49. IDictionary dic = (IDictionary)o;
  50. IDictionaryEnumerator enumerable = dic.GetEnumerator();
  51. enumerable.Reset();
  52. bool b = true;
  53. sb.Append("[");
  54. while(enumerable.MoveNext())
  55. {
  56. if(b){ b = false; }
  57. else{ sb.Append(","); }
  58.                 sb.Append('[');
  59. sb.Append(JavaScriptSerializer.Serialize(enumerable.Key));
  60. sb.Append(',');
  61. sb.Append(JavaScriptSerializer.Serialize(enumerable.Value));
  62.                 sb.Append(',');
  63.                 sb.Append(JavaScriptSerializer.SerializeString(enumerable.Key.GetType().FullName));
  64.                 sb.Append(',');
  65.                 sb.Append(JavaScriptSerializer.SerializeString(enumerable.Value.GetType().FullName));
  66.                 sb.Append(']');
  67. }
  68. sb.Append("]");
  69. return sb.ToString();
  70. }
  71.         public override Type[] DeserializableTypes
  72.         {
  73.             get
  74.             {
  75.                 return new Type[] { typeof(Hashtable) };
  76.             }
  77.         }
  78. public override Type[] SerializableTypes
  79. {
  80. get
  81. {
  82. return new Type[]{typeof(IDictionary), typeof(Hashtable)};
  83. }
  84. }
  85. }
  86. }