2

I have a question, I think it is basic knowledge but can not find any source of information to fill my gap.

Situation looks like this I am deserilizing couple of objects using for each. Foe example:

foreach (var property in _foundBackupProjectFiles)
{
    DeserializeDocumentSetFromBinary(property.Key);
}

Where _foundBackupProjectFiles is Dictionary Key is name of the object value is list of serilized files (names + path).

Now each object is a different list, for now I am using:

if (property == "Document")
{
    var deserilized = (List<DocumentSet.Document>) bformatter.Deserialize(stream);
    listdoc.AddRange(deserilized);
}
else if (property == "DocumentLookup")
{
    var deserilized = (List<DocumentSet.DocumentLookup>) bformatter.Deserialize(stream);
    listdoclookup.AddRange(deserilized);
}
else if (property == "DocumentText")
{
    var deserilized = (List<DocumentSet.DocumentText>) bformatter.Deserialize(stream);
    listdoctxt.AddRange(deserilized);
}

It works but it looks pretty ugly and hard to read, is there an elegant way to delegate this part :

var deserilized = List<DocumentSet.DocumentText>)bformatter.Deserialize(stream);
listdoctxt.AddRange(deserilized);

and add dynamic typing ?

slava
  • 1,901
  • 6
  • 28
  • 32
Wojciech Szabowicz
  • 3,646
  • 5
  • 43
  • 87
  • You could use reflection. The property seems to be the same as the class name, so you could look up the type and look if the deserialized object is a collection of that type... It would be useful if you plan to add more classes in the future - but it will not be faster OR easier to read - quite the contrary actually... unless your classes inherit from each other or inherit from a base class. – Adwaenyth Nov 12 '15 at 09:14
  • You can use this http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method – Alireza Nov 12 '15 at 09:22

1 Answers1

1

This is not a hell of a lot neater but you could use a switch with a extension method to add the deserialized object to the list.

public class Class
{
    private List<Document> listdoc = new List<Document>();
    private List<DocumentLookup> listdoclookup = new List<DocumentLookup>();
    private List<DocumentText> listdoctxt = new List<DocumentText>();

    public void Deserialize(string property, Stream stream)
    {
        var bformatter = new BinaryFormatter();
        var des = bformatter.Deserialize(stream);
        switch (property)
        {
            case "Document":
                listdoc.AddToList(des);
                break;

            case "DocumentLookup":
                listdoclookup.AddToList(des);
                break;

            case "DocumentText":
                listdoctxt.AddToList(des);
                break;

            default:
                throw new ArgumentOutOfRangeException("property");
        }
    }
}

public static class ListDeserializeExts
{
    public static void AddToList<TValue>(this List<TValue> list, object des)
    {
        list.AddRange((IEnumerable<TValue>)des);
    }
}
sQuir3l
  • 1,373
  • 7
  • 12