Skip to main content
private static void Visit(Control control, XmlDocument xml, XmlElement root)
{
    dynamic dynamicControl = control; //notice the 'dynamic' type.
                                      //this is the key to dynamic dispatch

    VisitCore(dynamicControl, xml, root);
}
private static void Visit(Control control, XmlDocument xml, XmlElement root)
{
    dynamic dynamicControl = control;
    VisitCore(dynamicControl, xml, root);
}
private static void Visit(Control control, XmlDocument xml, XmlElement root)
{
    dynamic dynamicControl = control; //notice the 'dynamic' type.
                                      //this is the key to dynamic dispatch

    VisitCore(dynamicControl, xml, root);
}
corrected spelling
Source Link

In our example above, it would use a virtual method in the object to convert, that calls a second virtual method in the object implementing the onversionconversion algorithm.

But that implies that the object upon which you want to apply the algorithm needs to collaborate with this: it needs to have support for the visitor pattern backedbaked in.

In our example above, it would use a virtual method in the object to convert, that calls a second virtual method in the object implementing the onversion algorithm.

But that implies that the object upon which you want to apply the algorithm needs to collaborate with this: it needs to have support for the visitor pattern backed in.

In our example above, it would use a virtual method in the object to convert, that calls a second virtual method in the object implementing the conversion algorithm.

But that implies that the object upon which you want to apply the algorithm needs to collaborate with this: it needs to have support for the visitor pattern baked in.

added 1591 characters in body
Source Link

UPDATE:

To apply the technique to your specific problem, first define your extension method:

public static XmlDocument ToXml(this Control control)
{
    XmlDocument xml = new XmlDocument();
    XmlElement root = xml.CreateElement(control.GetType().Name);
    xml.AppendChild(root);

    Visit(control, xml, root);

    return xml;
}

Create the dynamic dispatcher:

private static void Visit(Control control, XmlDocument xml, XmlElement root)
{
    dynamic dynamicControl = control;
    VisitCore(dynamicControl, xml, root);
}

Then fill in the specific methods:

private static void VisitCore(Control control, XmlDocument xml, XmlElement root)
{
    // TODO: specific Control handling
}

private static void VisitCore(ContainerControl control, XmlDocument xml, XmlElement root)
{
    // call the "base" method
    VisitCore(control as Control, xml, root);

    // TODO: specific ContainerControl handling
    // for example:
    foreach (Control child in control.Controls)
    {
        XmlElement element = xml.CreateElement(child.GetType().Name);
        root.AppendChild(element);

        // call the dynamic dispatcher method
        Visit(child, xml, element);
    }
}

private static void VisitCore(Form control, XmlDocument xml, XmlElement root)
{
    // call the "base" method
    VisitCore(control as ContainerControl, xml, root);

    // TODO: specific Form handling
}

UPDATE:

To apply the technique to your specific problem, first define your extension method:

public static XmlDocument ToXml(this Control control)
{
    XmlDocument xml = new XmlDocument();
    XmlElement root = xml.CreateElement(control.GetType().Name);
    xml.AppendChild(root);

    Visit(control, xml, root);

    return xml;
}

Create the dynamic dispatcher:

private static void Visit(Control control, XmlDocument xml, XmlElement root)
{
    dynamic dynamicControl = control;
    VisitCore(dynamicControl, xml, root);
}

Then fill in the specific methods:

private static void VisitCore(Control control, XmlDocument xml, XmlElement root)
{
    // TODO: specific Control handling
}

private static void VisitCore(ContainerControl control, XmlDocument xml, XmlElement root)
{
    // call the "base" method
    VisitCore(control as Control, xml, root);

    // TODO: specific ContainerControl handling
    // for example:
    foreach (Control child in control.Controls)
    {
        XmlElement element = xml.CreateElement(child.GetType().Name);
        root.AppendChild(element);

        // call the dynamic dispatcher method
        Visit(child, xml, element);
    }
}

private static void VisitCore(Form control, XmlDocument xml, XmlElement root)
{
    // call the "base" method
    VisitCore(control as ContainerControl, xml, root);

    // TODO: specific Form handling
}
added 125 characters in body
Source Link
Loading
Source Link
Loading