Although simple yet non-trivial that many a times we need to discover/load classes on runtime. A sample scenario could be that you have developed a lot of classes for different types of credit application processing and then you want different applications to be processed by different classes which you have developed (may be acquired from some one else).
Now the twist is that you do not want to hard-code the if else logic for extensibility/many other reasons i.e. the logic of routing a particular request to particular class etc.
Given the following:
You have one definitive way of deciding which request maps to which component/classYour handler components/classes follow an interface with a method e.g. ProcessRequest(XmlDocument doc)following is one way to deal with the problem.
C# Code Snippet
So lets say that you have a mapping of all the requests namespaces to their respective classes in your configuration files e.g. StudentCreditApplication maps to Namspace.Handler.StudentCredit assembly.
string completeName = ConfigurationSettings.AppSettings[requestXML.NamespaceURI];
/* QualifiedAssemblyNameKey is the name of your physical component that contains all the classes */
string assName = ConfigurationSettings.AppSettings[QualifiedAssemblyNameKey];
Assembly targetAssembly = Assembly.Load(assName);
Type classType = targetAssembly.GetType(completeName);
classType.ProcessRequest(requestXML);
There are many ways to deal with the above scenario and the one I have documented is just one of the many which works :) Its a very simplified version using .NET XmlDocument class as the request object but any other class may be used as well.
Lastly, I do realize that the snippet above has a lot of context information to it, however I am just trying to convey the idea here; please feel free to comment/question.
Example is not another way to teach; it is the only way to teach - Albert Einstein