Mixed Reality Toolkit Reflection Extension Method

I received a question a few weeks back about getting the Curved VR Keyboard to work on the HoloLens.

The error message appeared to be a reflection issue. UWP has some issues working with reflection because UWP is not managed. It is native and has a projection that allows us to write JavaScript apps or C# apps (and other languages) and it does the projection. This is really nice because we can write code using the language we like and deploy it to the Windows Store.

However, this means that some of the reflection APIs don’t quite work.

In particular, trying to call GetProperty or the like can cause issues. Fortunately, when it comes to Unity this is solved by the Mixed Reality Toolkit. There is a handy extension method that was created on the system’s Type class. The GetBaseType extension method will return the correct base type for UWP as well as when the code is running in the Unity Editor.

This is done using compiler directives (like #if UNITY_WSA && !UNITY_EDITOR ).

If you are trying to use a Unity Asset Store asset, like the Curved VR Keyboard, on the HoloLens or an immersive headset to run in the Windows Store, then you’ll want to avoid using reflection, or if it is needed, make sure you grab the appropriate base type.

Here is a video I created as I blindly tackled the problem.

Tell me what you think!