Từ 1 tới 4 trên tổng số 4 kết quả

Đề tài: BinaryFormatter.Serialize mà không DeSerialize được

  1. #1
    Ngày gia nhập
    08 2008
    Bài viết
    7

    Wink BinaryFormatter.Serialize mà không DeSerialize được

    Xin chào cả nhà
    Có ai bị như mình chưa.
    Tình hình như sau
    trong 1 project dạng Windows Application thì mình Serialize và DeSerialize bình thường với code được viết như sau:
    Visual C# Code:
    1. public void serialize(string filename, Oscillo os)
    2.         {
    3.             Stream stream = File.Open(filename, FileMode.Create);
    4.             BinaryFormatter bformatter = new BinaryFormatter();
    5.             bformatter.Serialize(stream, os);
    6.             stream.Close();
    7.         }
    8.  
    9.         // Deserialization oscillo object
    10.         public Oscillo deserialze(string filename)
    11.         {
    12.             Oscillo os = new Oscillo();
    13.             Stream stream = File.Open(filename, FileMode.Open);
    14.             BinaryFormatter bformatter = new BinaryFormatter();
    15.             os = (Oscillo)bformatter.Deserialize(stream);
    16.             stream.Close();
    17.             return os;
    18.         }
    Nhưng khi mình tạo 1 project mới dạng Windows Control Library thì đoạn code trên chỉ serialize được. Còn khi Deserialize thì báo lỗi
    Code:
    Unable to find assembly 'TekOsc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
    TekOsc là tên Windows Control Library project của mình
    đoạn này mình cũng nhìn thấy được trong file mà hàm Serialize tạo ra (nhìn bằng Notepad)
    Không thể pass được chỗ này.
    Ai có thể giải thích giùm mình với
    Thanks All for read.

  2. #2
    Ngày gia nhập
    07 2007
    Bài viết
    41

    Trước tôi cũng từ bị cái lỗi này. Có lẽ library của cậu được load động lên đúng không. Tôi thì bị lỗi khi deserialize dynamic type (type in dynamic library). Cậu có thể search trên mạng với các từ khóa: Deserialize Dynamic Reslove Assembly sẽ ra được vác cách giải quyết vấn đề.

    Nói chung cách giải quyết người ta đưa ra là bắt sự kiện AppDomain.CurrentDomain.AssemblyResolve và dựa vào cái EventArgs, nếu tên mà assembly nó cần là cái Assembly của minh thì return cái Assembly đó cho nó.

    Khi đó do đã dealine và hầu hết tất cả các kết quả tìm kiếm đều trả về cách này nêu tôi cũng chưa thèm test lại nữa. Nhưng tôi chỉ không hiểu đối với bài của tôi thì ngay trước thời điểm tôi Deserialize tôi check CurrentDomain thấy đã có cái Assembly tôi cần trong đó rồi, không hiểu sao nó không Resolve ra được nữa.

    Một số trang kết quả và code demo:
    Ref Links Code:

  3. #3
    Ngày gia nhập
    08 2008
    Bài viết
    7

    Trích dẫn Nguyên bản được gửi bởi zxc Xem bài viết
    Trước tôi cũng từ bị cái lỗi này. Có lẽ library của cậu được load động lên đúng không. Tôi thì bị lỗi khi deserialize dynamic type (type in dynamic library). Cậu có thể search trên mạng với các từ khóa: Deserialize Dynamic Reslove Assembly sẽ ra được vác cách giải quyết vấn đề.
    Cám ơn ZXC đã trả lời
    Mình giải quyết được rồi
    đối với việc Deserialize trong project windows control library của mình cần viết thêm 1 class có interface là SerializationBinder
    và sửa lại hàm Deserialize như sau:
    Visual C# Code:
    1. // new class
    2. sealed class Verbinder : SerializationBinder
    3.     {
    4.         public override Type BindToType(string assemblyName, string typeName)
    5.         {
    6.             Type typeToDeserialize = null;
    7.  
    8.             // The following line of code returns the type.
    9.             typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
    10.                 typeName, assemblyName));
    11.  
    12.             return typeToDeserialize;
    13.         }
    14.     }
    15.  
    16. // Deserialization oscillo object
    17.         public Oscillo deserialze(string filename)
    18.         {
    19.             Oscillo os = new Oscillo();
    20.             Stream stream = File.Open(filename, FileMode.Open);
    21.             BinaryFormatter bformatter = new BinaryFormatter();
    22.             bformatter.Binder = new Verbinder();    // Dong nay quan trong
    23.             // Neu dung voi WinForm thi khong can
    24.             // Khi project dang Control library thi can ^_^
    25.             os = (Oscillo)bformatter.Deserialize(stream);
    26.             stream.Close();
    27.             return os;
    28.         }
    29.     }
    Mọi chuyện lại Ok rồi
    Thanks again! ^_^

  4. #4
    Ngày gia nhập
    08 2008
    Bài viết
    7

    minh post toàn bộ code của mình lên đây cho bạn tham khảo nhé.
    Key ở đây chỉ là ở sealed class Verbinder : SerializationBinder
    Visual C# Code:
    1. {
    2.     [Serializable()]
    3.     public class OscChannel : ISerializable
    4.     {
    5.         public bool Status;        // Trang thai kenh tat hay bat
    6.         public string IdChannel;   // Ten kenh
    7.         public string Infor;       // Thong tin ve kenh
    8.         public double Pos;         // Vi tri kenh tren man hinh
    9.         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2500)]
    10.         public byte[] data;        // Du lieu cua kenh;
    11.  
    12.         public OscChannel()
    13.         {
    14.             Status = false;
    15.             IdChannel = "ch1";
    16.             Infor = " ";
    17.             Pos = 0.0;
    18.             data = new byte[2500];
    19.             for (int i = 0; i < 2500; i++) data[i] = 0;
    20.         }
    21.         //construction with copy data
    22.         public OscChannel(OscChannel channel)
    23.         {
    24.             Status = channel.Status;
    25.             IdChannel = channel.IdChannel;
    26.             Infor = channel.Infor;
    27.             Pos = channel.Pos;
    28.             data = new byte[2500];
    29.             for (int i = 0; i < 2500; i++) data[i] = channel.data[i];
    30.         }
    31.         public OscChannel(string id, string info)
    32.         {
    33.             Status = false;
    34.             IdChannel = id;
    35.             Infor = info;
    36.             Pos = 0.0;
    37.             data = new byte[2500];
    38.             for (int i = 0; i < 2500; i++) data[i] = 0;
    39.         }
    40.         // Deserialize
    41.         public OscChannel(SerializationInfo info, StreamingContext ctxt)
    42.         {
    43.             Status = (bool)info.GetValue("status", typeof(bool));
    44.             IdChannel = (String)info.GetValue("idchannel", typeof(string));
    45.             Infor = (String)info.GetValue("channelInfor", typeof(string));
    46.             Pos = (double)info.GetValue("position", typeof(double));
    47.             data = (byte[])info.GetValue("data", typeof(byte[]));
    48.         }
    49.         // Get object data
    50.         public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    51.         {
    52.             //You can use any custom name for your name-value pair. But make sure you
    53.             // read the values with the same name.
    54.             info.AddValue("status", Status);
    55.             info.AddValue("idchannel", IdChannel);
    56.             info.AddValue("channelInfor", Infor);
    57.             info.AddValue("position", Pos);
    58.             info.AddValue("data", data);
    59.         }
    60.     }
    61.     [Serializable()]
    62.     public class Oscillo : ISerializable
    63.     {
    64.         public string SysInfo;
    65.         public OscChannel chanel1;
    66.         public OscChannel chanel2;
    67.         public OscChannel chanel3;
    68.         public OscChannel chanel4;
    69.  
    70.         //Construction
    71.         public Oscillo()
    72.         {
    73.             SysInfo = "";
    74.             chanel1 = new OscChannel("ch1", "");
    75.             chanel2 = new OscChannel("ch2", "");
    76.             chanel3 = new OscChannel("ch3", "");
    77.             chanel4 = new OscChannel("ch4", "");
    78.         }
    79.         //construction with copy data
    80.         public Oscillo(Oscillo input)
    81.         {
    82.             SysInfo = input.SysInfo;
    83.             chanel1 = new OscChannel(input.chanel1);
    84.             chanel2 = new OscChannel(input.chanel2);
    85.             chanel3 = new OscChannel(input.chanel3);
    86.             chanel4 = new OscChannel(input.chanel4);
    87.         }
    88.  
    89.         // Deserialize
    90.         public Oscillo(SerializationInfo info, StreamingContext ctxt)
    91.         {
    92.             SysInfo = (string)info.GetValue("sysInfo", typeof(string));
    93.             chanel1 = (OscChannel)info.GetValue("channel1", typeof(OscChannel));
    94.             chanel2 = (OscChannel)info.GetValue("channel2", typeof(OscChannel));
    95.             chanel3 = (OscChannel)info.GetValue("channel3", typeof(OscChannel));
    96.             chanel4 = (OscChannel)info.GetValue("channel4", typeof(OscChannel));
    97.         }
    98.         public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    99.         {
    100.             info.AddValue("sysInfo", SysInfo);
    101.             info.AddValue("channel1", chanel1);
    102.             info.AddValue("channel2", chanel2);
    103.             info.AddValue("channel3", chanel3);
    104.             info.AddValue("channel4", chanel4);
    105.         }
    106.     }
    107.     class OscObject
    108.     {
    109.         //Oscillo oscillo;
    110.         public OscObject()
    111.         {
    112.             //oscillo = new Oscillo();
    113.         }
    114.         // Serialization oscillo object
    115.         public void serialize(string filename, Oscillo os)
    116.         {
    117.             Stream stream = File.Open(filename, FileMode.Create);
    118.             BinaryFormatter bformatter = new BinaryFormatter();
    119.             bformatter.Serialize(stream, os);
    120.             stream.Close();
    121.         }
    122.  
    123.         // Deserialization oscillo object
    124.         public Oscillo deserialze(string filename)
    125.         {
    126.             Oscillo os = new Oscillo();
    127.             Stream stream = File.Open(filename, FileMode.Open);
    128.             BinaryFormatter bformatter = new BinaryFormatter();
    129.             bformatter.Binder = new Verbinder();    // Dong nay quan trong
    130.             // Neu dung voi WinForm thi khong can
    131.             // Khi project dang Control library thi can ^_^
    132.             os = (Oscillo)bformatter.Deserialize(stream);
    133.             stream.Close();
    134.             return os;
    135.         }
    136.     }
    137.     // Tao Binder cho Binaryformatter
    138.     // Search in MSDN online (^_^)
    139.     sealed class Verbinder : SerializationBinder
    140.     {
    141.         public override Type BindToType(string assemblyName, string typeName)
    142.         {
    143.             Type typeToDeserialize = null;
    144.  
    145.             // For each assemblyName/typeName that you want to deserialize to
    146.             // a different type, set typeToDeserialize to the desired type.
    147.             String assemVer1 = "";// Assembly.GetExecutingAssembly().FullName;
    148.             String typeVer1 = "Version1Type";
    149.  
    150.             if (assemblyName == assemVer1 && typeName == typeVer1)
    151.             {
    152.                 // To use a type from a different assembly version,
    153.                 // change the version number.
    154.                 // To do this, uncomment the following line of code.
    155.                 // assemblyName = assemblyName.Replace("1.0.0.0", "2.0.0.0");
    156.  
    157.                 // To use a different type from the same assembly,
    158.                 // change the type name.
    159.                 typeName = "Version2Type";
    160.             }
    161.  
    162.             // The following line of code returns the type.
    163.             typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
    164.                 typeName, assemblyName));
    165.  
    166.             return typeToDeserialize;
    167.         }
    168.     }
    169. }
    Đã được chỉnh sửa lần cuối bởi hungdlbk : 21-09-2009 lúc 09:38 AM.

Các đề tài tương tự

  1. Serialize List, Dictionary của C# thành binary, Deserialize trong Java. Mong mọi người cùng thảo luận!
    Gửi bởi beginner2011 trong diễn đàn Thắc mắc lập trình Java
    Trả lời: 7
    Bài viết cuối: 16-09-2013, 01:26 PM
  2. Những lớp nào trong C# hỗ trợ Serialize
    Gửi bởi vungtroicuabo trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 1
    Bài viết cuối: 13-01-2013, 05:39 PM
  3. Làm sao để update file nhị phân = Serialize
    Gửi bởi eminter25391 trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 0
    Bài viết cuối: 11-06-2011, 09:02 PM
  4. Deserialize 1 binary file
    Gửi bởi javakhoqua trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 5
    Bài viết cuối: 17-12-2010, 04:42 PM
  5. serialize và deserialize một list<string> thành dạng xml?
    Gửi bởi kangoo1707 trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 5
    Bài viết cuối: 16-03-2010, 12:15 AM

Quyền hạn của bạn

  • Bạn không thể gửi đề tài mới
  • Bạn không thể gửi bài trả lời
  • Bạn không thể gửi các đính kèm
  • Bạn không thể chỉnh sửa bài viết của bạn