I am trying to get the properties of the FontWeights
class in C# using reflection.
var properties = typeof(FontWeights).GetProperties();
var dialog = new MessageDialog("Number of weights: " + properties.Length);
await dialog.ShowAsync();
When built with Debug configuration, the above works as expected. However, when using Release no properties are found.
Why is it so? Is there a way around it?
It's an UWP app.
It's an UWP app.
In the Release build your app is compiled with .NET Native. This is intentional, it ensures that you get to test the way the app will run on your user's machine. .NET Native is not exactly smooth sailing, it aggressively eliminates types from the final image to get the smallest possible binaries. That has sharp edges on code that normally needs the jitter to work properly. Reflection code in particular will bleed, like this code.
You need to help and tell the toolchain to include the FontWeights type into the final image. Open the Properties node of your project and double click Default.rd.xml. Add:
<Type Name="Windows.UI.Text.FontWeights" Dynamic="Required All" />
Rebuild and you'll see all is well now.