Developer Guide: Reflection
A convenient feature of the simulator is that particle algorithms do not have to be registered manually. The developer simply creates a new algorithm file using the template and the system will automatically detect the algorithm and make it available in the UI. This system is implemented using reflection.
The AlgorithmManager
class uses C#'s System.Reflection.Assembly
class to detect and manage all available ParticleAlgorithm
subclasses.
It finds the display name, the constructor, the Init
method and the referenced initialization class of each algorithm and stores all algorithms in a dictionary.
Algorithm classes that have duplicate names or no constructor with the correct signature are rejected.
The class also defines a list of excluded algorithms in which the names of algorithms that should not be shown in the UI can be specified.
The AlgorithmManager
uses a singleton pattern, meaning that one static instance is created when the application starts and is thereafter available to all other classes.
It is mainly used by the ParticleFactory
to create and initialize particles when a simulation starts.
A very similar system exists for InitializationMethod
subclasses: The InitializationMethodManager
follows the same approach as the AlgorithmManager
.
It provides the mapping from initialization method names to the actual classes that is needed to associate algorithms with their initialization methods.
It also uses reflection to get the parameter list of the initializer's Generate
method so that the parameters can be displayed in the Initialization Mode UI.