新凡

  博客园 :: 首页 :: :: 联系 :: 订阅 :: 管理 ::

Chat_CommonClass

  1 using System;
  2 using System.Runtime.InteropServices;
  3 using System.Text;
  4 using System.Windows.Forms;
  5 using System.IO;
  6 using System.Runtime.Serialization.Formatters.Binary;
  7 
  8 namespace JToolDemo.Tool_Chat
  9 {
 10     public static class Chat_CommonClass
 11     {
 12         #region 数据转换
 13         /// <summary>
 14         /// struct转换为byte[]
 15         /// </summary>
 16         /// <param name="structObj"></param>
 17         /// <returns></returns>
 18         public static byte[] StructToBytes(object structObj)
 19         {
 20             int size = Marshal.SizeOf(structObj);
 21             IntPtr buffer = Marshal.AllocHGlobal(size);
 22             try
 23             {
 24                 Marshal.StructureToPtr(structObj, buffer, false);
 25                 byte[] bytes = new byte[size];
 26                 Marshal.Copy(buffer, bytes, 0, size);
 27                 return bytes;
 28             }
 29             finally
 30             {
 31                 Marshal.FreeHGlobal(buffer);
 32             }
 33         }
 34 
 35         /// <summary>
 36         /// byte[]转换为struct
 37         /// </summary>
 38         /// <param name="bytes"></param>
 39         /// <param name="strcutType"></param>
 40         /// <returns></returns>
 41         public static object BytesToStruct(byte[] bytes, Type strcutType)
 42         {
 43             int size = Marshal.SizeOf(strcutType);
 44             IntPtr buffer = Marshal.AllocHGlobal(size);
 45             try
 46             {
 47                 Marshal.Copy(bytes, 0, buffer, size);
 48                 return Marshal.PtrToStructure(buffer, strcutType);
 49             }
 50             finally
 51             {
 52                 Marshal.FreeHGlobal(buffer);
 53             }
 54         }
 55         #endregion
 56 
 57         #region 方案二
 58         //2、序列化 
 59         public static byte[] ObjectToByteA(object obj)
 60         {
 61             MemoryStream fs = new MemoryStream();
 62             byte[] tmp = null;
 63             try
 64             {
 65                 // 序列化 
 66                 BinaryFormatter formatter = new BinaryFormatter();
 67                 formatter.Serialize(fs, obj);
 68                 tmp = fs.ToArray();
 69             }
 70             catch (Exception e)
 71             {
 72                 //MessageBox.Show(e.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
 73             }
 74             finally
 75             {
 76                 fs.Close();
 77             }
 78             return tmp;
 79         }
 80         //3、反序列化 
 81         public static object ByteAToObject(byte[] ba)
 82         {
 83             MemoryStream fs = new MemoryStream();
 84             object obj = null;
 85             try
 86             {
 87                 // 反序列化 
 88                 fs = new MemoryStream(ba);
 89                 fs.Position = 0;
 90                 BinaryFormatter formatter = new BinaryFormatter();
 91                 obj = formatter.Deserialize(fs);
 92             }
 93             catch (Exception e)
 94             {
 95                 MessageBox.Show(e.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
 96             }
 97             finally
 98             {
 99                 fs.Close();
100             }
101             return obj;
102         }
103         #endregion
104     }
105 }

 

posted on 2013-04-23 22:57  新凡  阅读(66)  评论(0)    收藏  举报