Picture the following situation. I have an XML document as follows,
<Form>
<Control Type="Text" Name="FirstName" />
<Control Type="DateTime" Name="DateOfBirth" />
<Control Type="Text" Name="PlaceOfBirth" />
</Form>
I have an abstract class called Control with a single abstract method called Process which takes a single parameter of HttpRequest. I also have two other classes that are derived from Control called TextControl and DateTimeControl. Both Text and DateTime override the Process method to provide their own implementations.
I also have a Form class which has a Process method taking a single parameter of type HttpRequest, and a constructor which takes a single parameter of type XmlDocument.
A new instance of Form is created and the above Xml is passed in via the XmlDocument parameter (how we get from the string to an XmlDocument is irrelevant). I then call the Process method on the instance of Form I just created and pass in a parameter of type HttpRequest as expected.
All good so far. Now onto the question.
To make the processing of Controls extensible I would like to be able to map Classes to control types.
eg.
Form.RegisterControl("Text", Text)
Form.RegisterControl("DateTime", DateTimeControl)
Within the Process method of Form I would like to itterate over each Control node in the document (how to do this is again irrelevant) and instantiate an instance of the class which matches its type based on the Classes registered by our RegisterControl method. I would be able at this stage to specify that they are derived from Control but could not explicitly specify their type. Because they are both derived from Control I want to call the Process method which I know will be implemented.
Is this even possible? If so how would I go about it?