I have certain classes that have common feature – class factory that I need to put into base class.
// This class needs to be designed
public class Base
{
// this function is no good because it returns object, and needs to return derived class
static object Create(byte[] data)
{
MemoryStream ms = new MemoryStream(data);
BinaryFormatter sf = new BinaryFormatter();
object result = sf.Deserialize(ms);
ms.Close();
return result;
}
public virtual byte[] Serialize()
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, this);
byte[] result = ms.ToArray();
ms.Close();
return result;
}
}
// for this class Create() must return Derived1
public Derived1 : Base
{
}
// for this class Create() must return Derived2
public Derived2 : Base
{
}
I need to be able to use derived class factory like that
Derived1 d1 = Derived1.Create(data1);
Derived2 d2 = Derived2.Create(data2);
Please advice solution. The templates are ok
UPDATE
I update the code and showed how objects are serialized and deserialized
1 Answer