如题;
由于工作需要,需要使用webservice序列化泛型集合;本文只是测试所用范例;正式代码就不贴了;
首先 序列化类 SerializableObj<T>
代码
1 using System;
2 using System.IO.Compression;
3 using System.IO;
4 using System.Collections.Generic;
5 using System.Runtime.Serialization.Formatters.Binary;
6 using System.Collections;
7 /// <summary>
8 /// SerializableObj 的摘要说明
9 /// </summary>
10 public class SerializableObj<T> where T : IEnumerable
11 {
12 public static Byte[] Serialize(T obj)
13 {
14 try
15 {
16 MemoryStream ms = new MemoryStream();
17 BinaryFormatter bf = new BinaryFormatter();
18 bf.Serialize(ms, obj);
19 ms.Position = 0;
20 byte[] buffers = ms.ToArray();
21 ms.Close();
22 ms.Dispose();
23 return Compress(buffers);
24 }
25 catch (Exception ex)
26 {
27
28 throw ex;
29 }
30 }
31
32 public static byte[] Compress(byte[] buffer)
33 {
34 MemoryStream ms = new MemoryStream();
35 GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true);
36 //byte[] returnBuffer=new byte [];
37 gzip.Write(buffer, 0, buffer.Length);
38 gzip.Close();
39 ms.Position = 0;
40 byte[] rebuffer = new byte[ms.Length];
41 ms.Read(rebuffer, 0, int.Parse(ms.Length.ToString()));
42 return rebuffer;
43 }
44
45
46 }
47 [Serializable]
48 public class MyList<T> : List<T>
49 {
50
51 }
webservice:
代码
1 using System;
2 using System.Web;
3 using System.Collections;
4 using System.Web.Services;
5 using System.Web.Services.Protocols;
6
7
8 /// <summary>
9 /// CredentiaWebSer 的摘要说明
10 /// </summary>
11 [WebService(Namespace = "http://tempuri.org/")]
12 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
13 [System.ComponentModel.ToolboxItem(false)]
14
15 public class CredentiaWebSer : System.Web.Services.WebService {
16
17 [WebMethod]
18 public byte[] GetPassingObj()
19 {
20 Passing pa = new Passing();
21 pa.Like = "wwe";
22 pa.Age = 23;
23 pa.Name = "常遇春";
24 Passing p2 = new Passing();
25 p2.Like = "music";
26 p2.Age = 24;
27 p2.Name = "傅红雪";
28 MyList<Passing> list = new MyList<Passing>();
29 list.Add(pa);
30 list.Add(p2);
31 byte[] bytes=SerializableObj<MyList<Passing>>.Serialize(list);
32 return bytes;
33 }
34 }
35
36
客户端 反序列化类
代码
1 using System;
2 using System.IO;
3 using System.IO.Compression;
4 using System.Runtime.Serialization.Formatters.Binary;
5 using System.Collections.Generic;
6 using System.Collections;
7
8 /// <summary>
9 /// Deserizlize 的摘要说明
10 /// </summary>
11 public class Deserizlize<T> where T : class,IEnumerable
12 {
13 public Deserizlize()
14 {
15 //
16 // TODO: 在此处添加构造函数逻辑
17 //
18 }
19 public static byte[] Decompress(byte[] buffer)
20 {
21 try
22 {
23 MemoryStream ms = new MemoryStream(buffer);
24 ms.Position = 0;
25 GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress);
26 byte[] rebuffer = new byte[100];
27 MemoryStream msre = new MemoryStream();
28 int readcont = gzip.Read(rebuffer, 0, rebuffer.Length);
29 while (readcont > 0)
30 {
31 msre.Write(rebuffer, 0,readcont);
32 readcont= gzip.Read(rebuffer,0,rebuffer.Length);
33 }
34 return msre.ToArray();
35 }
36 catch (Exception ex)
37 {
38
39 throw ex;
40 }
41 }
42
43 public static T Deserilize(byte[] buffer)
44 {
45 try
46 {
47 byte[] debuffer = Decompress(buffer);
48 BinaryFormatter bf = new BinaryFormatter();
49 MemoryStream ms = new MemoryStream(debuffer);
50 ms.Position = 0;
51 T obj = bf.Deserialize(ms) as T;
52 return obj;
53 }
54 catch (Exception ex)
55 {
56
57 throw ex;
58 }
59 }
60 }
61 [Serializable]
62 public class MyList<T> : List<T>
63 {
64
65 }
客户端调用:
代码
1 cyc.CredentiaWebSer ser = new cyc.CredentiaWebSer();
2 ser.AuthorityWebSerValue = header;
3 byte[] buffer = ser.GetPassingObj();
4 MyList<Passing> list = Deserizlize<MyList<Passing>>.Deserilize(buffer);
5 foreach (Passing var in list)
6 {
7 Response.Write(var.Show()+"\n");
8 }