Memory 反序列化Revit API
这是我第一次接触到序列化……事实上,我正在通过C#开发Autodesk Revit 目标:Memory 反序列化Revit API,memory,serialization,stream,revit-api,disposable,Memory,Serialization,Stream,Revit Api,Disposable,这是我第一次接触到序列化……事实上,我正在通过C#开发Autodesk Revit 目标: ///main class ..... ..... ThrDviewData v3ddata = new ThrDviewData(); ///instantiate a serializable class ///collect all required data string filename = UT_helper.conpaths(UT_constants.paths.Desktop) +
///main class
.....
.....
ThrDviewData v3ddata = new ThrDviewData(); ///instantiate a serializable class
///collect all required data
string filename = UT_helper.conpaths(UT_constants.paths.Desktop) + "\\comment2" + DateTime.Today.ToShortTimeString().Replace(":", "") + ".a4h";
using (Stream stream = File.Open(filename, FileMode.Create))
{
BinaryFormatter bformatter = new BinaryFormatter();
Debug.WriteLine("Writting Data\r\n");
bformatter.Serialize(stream, v3ddata);
stream.Close();
}
v3ddata = null;
using (Stream stream = File.Open(filename, FileMode.Open))
{
BinaryFormatter bformatter = new BinaryFormatter();
Debug.WriteLine("Reading data from file");
try
{
v3ddata = (ThrDviewData)bformatter.Deserialize(stream);
}
catch (Exception ex)
{
Debug.Write(ex.Message);
// File.Delete(filename);
}
stream.Close();
}
....
....
///do some stuff with the acquired data
我需要将数据记录到硬盘上的新文件中,以便可以通过Revit从另一台计算机打开此文件
程序:
///main class
.....
.....
ThrDviewData v3ddata = new ThrDviewData(); ///instantiate a serializable class
///collect all required data
string filename = UT_helper.conpaths(UT_constants.paths.Desktop) + "\\comment2" + DateTime.Today.ToShortTimeString().Replace(":", "") + ".a4h";
using (Stream stream = File.Open(filename, FileMode.Create))
{
BinaryFormatter bformatter = new BinaryFormatter();
Debug.WriteLine("Writting Data\r\n");
bformatter.Serialize(stream, v3ddata);
stream.Close();
}
v3ddata = null;
using (Stream stream = File.Open(filename, FileMode.Open))
{
BinaryFormatter bformatter = new BinaryFormatter();
Debug.WriteLine("Reading data from file");
try
{
v3ddata = (ThrDviewData)bformatter.Deserialize(stream);
}
catch (Exception ex)
{
Debug.Write(ex.Message);
// File.Delete(filename);
}
stream.Close();
}
....
....
///do some stuff with the acquired data
可序列化类
public string myvariables;
public ThrDviewData()
{
myvariables = null;
}
public ThrDviewData(SerializationInfo info, StreamingContext ctxt)
{
myvariables= (String)info.GetValue("name", typeof(string));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("name", myvariables);
}
// Public implementation of Dispose pattern callable by consumers.
public void Dispose()
{
GC.SuppressFinalize(this);
}
}
有什么提示吗?您正在使用的二进制序列化程序与您正在导出的类紧密相连。 当您使用Revit加载项管理器加载加载项时,加载项管理器将创建部件的动态副本(以便您可以在同一会话中再次加载部件)。当它这样做时,您将得到具有相同名称的重复类型(ThrDviewData)。当您尝试加载来自不同副本的以前序列化的二进制文件时,它仍在尝试映射到原始类型(而不是该类型的新副本) 你的选择是: 1.不要使用加载项管理器,只需静态使用加载项即可。 2.使用与类型不紧密耦合的二进制序列化程序以外的其他程序(如XML或JSON序列化程序,如您所尝试的)
事情就是这样…您如何加载加载项?您是否使用加载项管理器或类似的工具来动态加载代码?我觉得我以前见过这个问题,当你在内存中同时加载了两个代码的“副本”时。是的!通过外接程序管理器。奇怪的是,我遵循了相同的过程,但通过Json,它工作得很有效。