Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type.GetProperties() doesn't work in Release

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.

like image 850
holmis83 Avatar asked May 03 '26 12:05

holmis83


1 Answers

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.

like image 158
Hans Passant Avatar answered May 06 '26 00:05

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!