jeudi 19 octobre 2017

Replace conditional with polimorphism how to

I would like to replace the if statements in the following recursive function with polimorphism.

i read alot about it, see several youtube videos but still, cannot see the way of actually doing it on my code (that was simplified for the purpose of this post)

what makes this task more difficult for me is the presence of a foreach statment at the begining of the function and the recursive call

thanks for the help

public void FlattenXml(XElement xml, string id = null)
{
    var elements = xml.Elements().ToList();
    foreach (var element in elements)
    {
        if (element.Name == "name1")
        {
            Func1();
        }
        if (element.Name == "name2")
        {
            Func2();
        }
        if (element.Name == "name3")
        {
            DoSomethingElse();
            FlattenXml(content, tempId);
            Func3();
        }
        else
        {
            DoSomethingCompletelyDifferent();
            FlattenXml(element, id);
        }
    }
    xml.Elements("name3").Remove();
}

Aucun commentaire:

Enregistrer un commentaire