我有一个客户端/服务器项目,我正在尝试通过套接字将 DataTable(从 TableAdapter 中提取)从服务器发送到客户端。我的服务器命名空间是 srvCentral,我的客户端是 appClient。当我尝试在客户端反序列化 DataTable 时,它会抛出一个序列化异常,提示无法找到程序集 'srvCentral,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null' svchost 挂起并强制我关闭,并使用这样的活页夹:
sealed class AllowAllAssemblyVersionsDeserializationBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
Type typeToDeserialize = null;
String currentAssembly = Assembly.GetExecutingAssembly().FullName;
// In this case we are always using the current assembly
assemblyName = currentAssembly;
// Get the type using the typeName and assemblyName
typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
typeName, assemblyName));
return typeToDeserialize;
}
}
异常仍然存在......难道不是假设 DataTable 在任何地方都可以反序列化吗?我究竟做错了什么?
我的序列化代码是:
public byte[] Serializar(Object item)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms, item);
return ms.ToArray();
}
public Object Deserializar(byte[] buffer)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream(buffer);
formatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
formatter.Binder = new AllowAllAssemblyVersionsDeserializationBinder();
Object a = formatter.Deserialize(ms);
return a;
}
sealed class AllowAllAssemblyVersionsDeserializationBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
Type typeToDeserialize = null;
String currentAssembly = Assembly.GetExecutingAssembly().FullName;
// In this case we are always using the current assembly
assemblyName = currentAssembly;
// Get the type using the typeName and assemblyName
typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
typeName, assemblyName));
return typeToDeserialize;
}
}
经过一番挖掘,我已经解决了这个问题;
这不是解决问题的更好方法,而是避免问题的简单方法。对于小事来说就足够了。如果您只想从表中检索数据以显示内容,请使用此选项。
namespace YourLibrary
{
[Serializable]
public class Tabela: ISerializable
{
protected ArrayList colNames;
protected ArrayList colTypes;
protected ArrayList dataRows;
public Tabela()
{
}
public Tabela (DataTable dt)
{
colNames = new ArrayList();
colTypes = new ArrayList();
dataRows = new ArrayList();
// Insert column information (names and types)
foreach(DataColumn col in dt.Columns)
{
colNames.Add(col.ColumnName);
colTypes.Add(col.DataType.FullName);
}
// Insert rows information
foreach(DataRow row in dt.Rows)
dataRows.Add(row.ItemArray);
}
public Tabela(SerializationInfo info, StreamingContext context)
{
colNames = (ArrayList)info.GetValue("colNames",typeof(ArrayList));
colTypes = (ArrayList)info.GetValue("colTypes",typeof(ArrayList));
dataRows = (ArrayList)info.GetValue("dataRows",typeof(ArrayList));
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("colNames", colNames);
info.AddValue("colTypes", colTypes);
info.AddValue("dataRows", dataRows);
}
public DataTable GenerateDataTable()
{
DataTable dt = new DataTable();
// Add columns
for(int i=0; i<colNames.Count; i++)
{
DataColumn col = new DataColumn(colNames[i].ToString(),
Type.GetType(colTypes[i].ToString() ));
dt.Columns.Add(col);
}
// Add rows
for(int i=0; i<dataRows.Count; i++)
{
DataRow row = dt.NewRow();
row.ItemArray = (object[]) dataRows[i];
dt.Rows.Add(row);
}
dt.AcceptChanges();
return dt;
}
}
}