客户端
main()
{
string[] temp = CommonFunction.SerializeDBObject(dbobject);
WebService dataService = new WebService();
int nResult = dataService.AddObject(temp);
}
public static string[] SerializeDBObject(DBObject ds)
{
// 序列化对象
BinaryFormatter binaryFormatter = new BinaryFormatter();
// 创建一个内存流,序列化后保存在其中
MemoryStream ms = new MemoryStream();
byte[] b;
// 将DBObject对象序列化为内存流
//
binaryFormatter.Serialize(ms, ds);
// 设置内存流的起始位置
//
ms.Position = 0;
// 读入到 byte 数组
//
b = new Byte[ms.Length];
ms.Read(b, 0, b.Length);
ms.Close(); string[] temp = new string[b.Length];
for(int i = 0 ; i < b.Length ; i++)
{
temp[i] = b[i].ToString();
}
return temp;
} 服务端:
[WebMethod]
public int AddObject(string[] b)
{
DBObject lawobject = DeserializeDBObject(b);
return DbObjectAccess.AddObject(lawobject);
}public DBObject DeserializeDBObject(string[] objectString)
{
DBObject db= new DBObject(); byte[] serializedDBObject = new byte[objectString.Length];; for(int i = 0 ; i < objectString.Length ; i++)
serializedDBObject[i] = byte.Parse(objectString[i]); if (serializedDBObject.Length == 0)
return null;
try
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
// 将 byte 数组到内存流
//
ms.Write(serializedDBObject, 0, serializedDBObject.Length);
// 将内存流的位置到最开始位置
//
ms.Position = 0;
// 反序列化成DBObject对象,创建出与原对象完全相同的副本
//
db = (DBObject) binaryFormatter.Deserialize(ms);
ms.Close(); return db;
}
catch(Exception ex)
{
return null;
}
} 现在运行系统提示:无法找到程序集请教......
main()
{
string[] temp = CommonFunction.SerializeDBObject(dbobject);
WebService dataService = new WebService();
int nResult = dataService.AddObject(temp);
}
public static string[] SerializeDBObject(DBObject ds)
{
// 序列化对象
BinaryFormatter binaryFormatter = new BinaryFormatter();
// 创建一个内存流,序列化后保存在其中
MemoryStream ms = new MemoryStream();
byte[] b;
// 将DBObject对象序列化为内存流
//
binaryFormatter.Serialize(ms, ds);
// 设置内存流的起始位置
//
ms.Position = 0;
// 读入到 byte 数组
//
b = new Byte[ms.Length];
ms.Read(b, 0, b.Length);
ms.Close(); string[] temp = new string[b.Length];
for(int i = 0 ; i < b.Length ; i++)
{
temp[i] = b[i].ToString();
}
return temp;
} 服务端:
[WebMethod]
public int AddObject(string[] b)
{
DBObject lawobject = DeserializeDBObject(b);
return DbObjectAccess.AddObject(lawobject);
}public DBObject DeserializeDBObject(string[] objectString)
{
DBObject db= new DBObject(); byte[] serializedDBObject = new byte[objectString.Length];; for(int i = 0 ; i < objectString.Length ; i++)
serializedDBObject[i] = byte.Parse(objectString[i]); if (serializedDBObject.Length == 0)
return null;
try
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
// 将 byte 数组到内存流
//
ms.Write(serializedDBObject, 0, serializedDBObject.Length);
// 将内存流的位置到最开始位置
//
ms.Position = 0;
// 反序列化成DBObject对象,创建出与原对象完全相同的副本
//
db = (DBObject) binaryFormatter.Deserialize(ms);
ms.Close(); return db;
}
catch(Exception ex)
{
return null;
}
} 现在运行系统提示:无法找到程序集请教......
byte[] 可以在webSerive 传递的!而且string 比 byte 大的!
服务端和客户端都要有这个dll并且引用了。
如果 传递的是继承了 DBObject 的子类,子类
也要两边都有!
至于byte[]改成string[]是因为我原来这么做的时候.net报错,后来调了一下又可以了...
多谢...