partial or full object serialization

前端 未结 2 683
孤街浪徒
孤街浪徒 2021-01-27 08:54

Consider the following Student defintion:

public class Student
{
    public Guid Id {get; set;}
    public String FirstName {get; set;}
    public S         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-27 09:48

    I don't know what kind of serialization are you talking about. But if you use the BinaryFormatter, the way to go is to make the Student class implement the ISerializable.

    Then, you can do something like:

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
      info.AddValue("Id", Id);
    
      if (SomeCondition)
      {
        info.AddValue("FirstName", FirstName);
        info.AddValue("LastName", LastName);
      }
    }
    

    Where SomeCondition may use a static (or thread-static) variable or some information inside the StreamingContext.

提交回复
热议问题