当前位置:编程学习 > C#/ASP.NET >>

C# 自己写的一个类,用来将结构体或类中的数据打成"数据包",进行网络发送

using System;
using System.Reflection;
using System.Net;
using System.Net.Sockets;

using System.IO;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
namespace CSProxy
{
 /// <summary>
 /// CSProxyTrans 转换有byte,ushort,int 和 string 类型成员的结构体,并且string 类型只是4字节的IP地址。
 /// 例:
 /// Login login=new Login();
 /// login.Flage=1;
 /// login.PassWord="zzy";
 /// login.State=1;
 /// login.UserID="zhaozhonglei";
 ///
 /// byte[] buffer=Trans.ToBytes("jy.P2PBLL.Login",login);
 /// Login  login2=(Login)Trans.ToStruct("jy.P2PBLL.Login",buffer);
 /// </summary>
 
 public class CSProxyTrans
 {
  /// <summary>
  /// 将成员转化为字节数组
  /// </summary>
  /// <param name="thetype">类型的完全限定名,如jy.P2PBLL.Login</param>
  /// <param name="obj">该类型的对象</param>
  /// <returns>含有个格式的字节数组</returns>
  public static byte[] ToBytes(string thetype,object obj)
  {
   Type type=Type.GetType(thetype);
   FieldInfo [] infos=type.GetFields();

   byte[] buffer=new byte[10240];
   int    bp=0;

   foreach(FieldInfo fi in infos)
   {
    string a=fi.FieldType.ToString();

    string b=typeof(byte).ToString();
    string us=typeof(ushort).ToString();
    string n=typeof(int).ToString();
    string s=typeof(string).ToString();
    
    if(a == b)
    {
     buffer[bp]=(byte)fi.GetValue(obj);
     bp++;
    }
    if(a == us)
    {
     byte[] uShort=BitConverter.GetBytes(IPAddress.HostToNetworkOrder((ushort)fi.GetValue(obj)));//转化成网络字节顺序
     uShort.CopyTo(buffer,bp);
     bp+=2;
    }
    if(a == n)
    {
     byte[] bint=BitConverter.GetBytes((int)fi.GetValue(obj));//暂时不转,因用来盛放一个 IP
     bint.CopyTo(buffer,bp);
     bp+=4;
    }

    if(a == s)
    {
     object O= fi.GetValue(obj);
     string str=(string)O;
     if(O!=null)
     {
      byte[] bstring=System.Text.Encoding.Unicode.GetBytes(str);
      int    len=bstring.Length;
      byte[] bint=BitConverter.GetBytes(len);

      bint.CopyTo(buffer,bp);
      bp+=4;

      bstring.CopyTo(buffer,bp);
      bp+=len;
     }
     else
     {
      byte[] bint=BitConverter.GetBytes(0);

      bint.CopyTo(buffer,bp);
      bp+=4;
     }
    }
   }

   byte[] data=new byte[bp];
   Array.Copy(buffer,0,data,0,bp);

   return data;
  }

  /// <summary>
  /// 得到thetype类型的对象
  /// </summary>
  /// <param name="thetype">类型的完全限定名,如jy.P2PBLL.Login</param>
  /// <param name="data">含有个格式的字节数组</param>
  /// <returns>thetype类型的对象</returns>
  public static object ToStruct(string thetype,byte [] data)
  {
   Type type=Type.GetType(thetype);
   FieldInfo [] infos=type.GetFields();
   
   object obj=Activator.CreateInstance(type);
   int    bp=0;

   foreach(FieldInfo fi in infos)
   {
    string a=fi.FieldType.ToString();

    string b=typeof(byte).ToString();
    string us=typeof(ushort).ToString();
    string n=typeof(int).ToString();
    string s=typeof(string).ToString();

    if(a == b)
    {
     byte bval=data[bp];
     fi.SetValue(obj,bval);
     bp+=1;
     
    }
    if(a == us)
    {
     ushort be=BitConverter.ToUInt16(data,bp);
     ushort sval=(ushort)IPAddress.NetworkToHostOrder((short)be);
     fi.SetValue(obj,sval);
     bp+=2;     
    }
    if(a == n)
    {
     int val=BitConverter.ToInt32(data,bp);
     fi.SetValue(obj,val);
     bp+=4;
    }

    if(a == s)
    {
     int len=BitConverter.ToInt32(data,bp);
     bp+=4;
     if(len!=0)
     {
      string val=System.Text.Encoding.Unicode.GetString(data,bp,len);
      fi.SetValue(obj,val);
      bp+=len;
     }
     else
     {
      string val="";
      fi.SetValue(obj,val);
     }
    }
   }
   return  obj;
  }

 

  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  public static unsafe byte[] ToBytes(object obj)
  {
   int size = Marshal.SizeOf(obj);
   byte[] bytes = new b

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,