Type.GetProperties() doesn't work in Release

三世轮回 提交于 2019-12-21 05:34:26

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/35359942/type-getproperties-doesnt-work-in-release

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!