I have a class in my Prism/CAL application which generates a form for users to fill in data.
The form is defined by an XML file like this:
<area idCode="general" title="General">
<column>
<group title="Customer Data">
<field idCode="title" requiredStatus="true">
<label>title</label>
<fieldType>Title</fieldType>
</field>
<field idCode="firstName" requiredStatus="true">
<label>First Name</label>
<fieldType>Text</fieldType>
</field>
<field idCode="lastName" requiredStatus="true">
<label>Last Name</label>
<fieldType>Text</fieldType>
</field>
<field idCode="email" requiredStatus="true">
<label>E-Mail</label>
<fieldType>Email</fieldType>
</field>
...
</group>
</column>
</area>
the form needs to load specific controls which correspond to each field type in the XML, e.g.
- Title (shows a dropdown: Mr., Mrs., Dr., etc.)
- Text (simple textbox)
- Email (textbox with e-mail validation)
- ZipCode (textbox with zipcode validation)
I want to make each control a separate module which gets loaded so that e.g. the ZipCode module would exist in a Modules directory as a file:
ZipCode.dll
which is just a simple textbox control that validates based on zipcode, but developers could make another control called:
ZipCodePlus.dll
which inherits the same interface but provides a pop-up geo-earth selector for zipcodes. As soon as a customer replaced ZipCode.dll with ZipCodePlus.dll, all of his forms would have this new functionality for searching for zip codes.
However, I'm having trouble visualizing how this will be technically implemented, since as my form class is parsing the XML, it instantiates the classes which provide the functionality for the controls, but in order to instantiate the class, I have to have a reference to it:
SmartFormFieldZipCodePresenter smartFormFieldEmailPresenter
= container.Resolve<SmartFormFieldEmailPresenter>();
But how can I instantiate it dynamically, i.e. with the name of the class as a string, and if that class does not exist then it will throw an appropriate exception, e.g. something like this:
PSEUDO-CODE:
try {
var smartFormFieldZipCodePresenter
= container.Resolve("smartFormFieldZipCodePresenter");
}
catch (ModuleDoesNotExistException) {
...
}