0

On my code i hold some collection of ISerialize.

 Dictionary<int, ISerialize>   // the int is the key and its mean the ID  

And i have 2 type of object =>

public interface ISerialize
{
     byte[] Serialize();
     ISerialize DeSerialize();
}

class Elem1 : ISerialize
{
   public long ID { get; set; }
   public string Name { get; set; }

   public byte[] Serialize()
   {
       // Serialize implement 
   }

   public ISerialize DeSerialize()
   {
       // DeSerialize implement 
   }
}

class Elem2 : ISerialize
{
   public long ID { get; set; }
   public string Name1 { get; set; }
   public string Name2 { get; set; }
   public int Age { get; set; }

   public byte[] Serialize()
   {
       // Serialize implement 
   }

   public ISerialize DeSerialize()
   {
       // DeSerialize implement 
   }
}

I want to Serialize and DeSerialize all the Dictionary.
So i know how to do the Serialize ( just go with foreach and create continues byte[] )

But when i doing the DeSerialize i can't know what kind of type i dealing with ( Elem1 or Elem2 ) and i can't actually to do the DeSerialize.

How to solve this ? What i the best practice on this ?

Yanshof
  • 9,659
  • 21
  • 95
  • 195
  • Where do you plan on putting the result? Another dictionary? Or a list? – Robert Harvey Apr 19 '16 at 05:35
  • When i doing DeSerialize i plan to put again on dictionary – Yanshof Apr 19 '16 at 05:38
  • Implement `ISerializable` instead. See [here](https://msdn.microsoft.com/en-us/library/ty01x675%28v=vs.110%29.aspx). If you don't need custom serialization (i.e. all your classes are just property bags), then you can use ordinary XML serialization and deserialization to do this, and you don't even need a custom serialization interface. – Robert Harvey Apr 19 '16 at 05:39
  • 1
    Another good read. http://stackoverflow.com/questions/4659248/serializing-interfaces. Using buiness objects over the process boundry are typically a bad idea. That extra work you need to do getting rid of the interfaces will aid you greatly in the long run. – jgauffin Apr 19 '16 at 05:45

0 Answers0