MvvmCross utilizes reflection extensively during object construction, particularly through its Inversion of Control (IoC) container, which is managed by the `Mvx.IoCProvider` singleton instance. Here's a detailed overview of how MvvmCross handles reflection during object construction:
Constructor Injection
MvvmCross supports constructor injection, which allows it to automatically resolve parameters during object construction using reflection. When creating an object, MvvmCross performs the following steps:
1. Reflection to Find the Constructor: MvvmCross uses reflection to identify the constructor of the class being instantiated.
2. Parameter Resolution: It examines the parameters required by the constructor and identifies the interfaces or types needed.
3. Resolve Registered Implementations: For each parameter, MvvmCross uses `Mvx.IoCProvider.Resolve()` to obtain the registered implementation for the interface or type.
4. Invoke Constructor: Once all parameters are resolved, MvvmCross uses reflection to invoke the constructor with the resolved parameters.
This process is recursive, meaning if any of the resolved objects also require other objects, MvvmCross will resolve those as well[1][4].
Bulk Registration by Convention
MvvmCross also supports bulk registration of classes using reflection. This is typically done in the `App` class, which inherits from `MvxApplication`. The framework uses reflection to find all classes in the Core assembly that end with "Service" and registers them as lazy singletons. This is achieved through the following steps:
1. Find Classes Ending with "Service": Reflection is used to find all classes with names ending in "Service" within the Core assembly.
2. Identify Interfaces: For each class found, MvvmCross identifies the interfaces it implements.
3. Register as Lazy Singletons: These classes are then registered as lazy singletons against their respective interfaces. This means they are not instantiated until they are first requested[2][4][7].
ViewModels and Dependency Injection
MvvmCross uses the same constructor injection mechanism for ViewModels. When creating a ViewModel, MvvmCross resolves any dependencies specified in the constructor using the IoC container. This allows for easy customization of services across different platforms and facilitates unit testing by enabling the use of mock implementations[1][4].
Customization and AppStart
The `App` class is responsible for registering custom objects on the IoC container and starting the application's ViewModels. If needed, you can customize the startup process by using a custom `AppStart` class to decide which ViewModel to present first[2][7].
In summary, MvvmCross leverages reflection to automate the process of resolving dependencies during object construction, making it easier to manage complex object graphs and support cross-platform development with minimal platform-specific code.
Citations:[1] https://www.mvvmcross.com/documentation/fundamentals/dependency-injection
[2] https://yanxiaodi.gitbook.io/xamarin-mvvmcross-handbook/using-mvvmcross-to-create-your-first-xamarin-app/creating-the-core-project/adding-an-app-class
[3] http://wojosoftware.com/software/mvvmcross/
[4] https://www.mvvmcross.com/documentation/fundamentals/inversion-of-control-ioc
[5] https://www.codeproject.com/Articles/863695/Introduction-to-Mvvmcross-Platform
[6] https://github.com/media-tools/sqlite-net-extensions
[7] https://www.mvvmcross.com/documentation/getting-started/mvvmcross-overview
[8] https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle
[9] https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/august/windows-phone-build-mvvm-apps-with-xamarin-and-mvvmcross
[10] https://www.mvvmcross.com/documentation/advanced/customizing-using-App-and-Setup