Is there any design patterns to Read and write of different shapes stored in List<Shape>.

Posted by Debiprasadghosh under C# on 7/1/2011 | Points: 10 | Views : 1350 | Status : [Member] | Replies : 1
I have one abstract Shape class. Different Line, Curve, Rectangle, Square classes are derived from Shape.

one variable List<Shape> is storing different objects of these classes.

Writing this variable to a file is straight forward.
But, reading from file to fill this variable needs to know what kind of class is going to be read next. So, some signature should be written in the file appriory .....

If any standard technique is available for serialization for inheritance.


Posted by: Debiprasadghosh on: 7/1/2011 [Member] Starter | Points: 25

I got indirect answer

I have to give the signature of GetType().AssemblyQualifiedName at the time of Serialization as string

and at the time of deserialization I can create class of derived type using following reflection.

public static class DerivedClass
public static object Create(string assemblyQualifiedName)
Type tp = Type.GetType(assemblyQualifiedName);
ConstructorInfo c = tp.GetConstructor(new Type[0]);
return (object)c.Invoke(BindingFlags.NonPublic, null, null, null);

It is for without parameter constructor.
Anyway, similarly the case for uniform arguments constractor over the inhertance chanel can be created. But I am prefering this one.

Debiprasadghosh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response