Type.GetProperties() 在 Release 中不起作用 [英] Type.GetProperties() doesn't work in Release

查看:22
本文介绍了Type.GetProperties() 在 Release 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用反射在 C# 中获取 FontWeights 类的属性.

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();

当使用 Debug 配置构建时,上述内容按预期工作.但是,在使用 Release 时找不到任何属性.

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?

这是一个 UWP 应用.

It's an UWP app.

推荐答案

这是一个 UWP 应用.

It's an UWP app.

在发布版本中,您的应用是使用 .NET Native 编译的.这是有意为之,它确保您可以测试应用程序在用户计算机上的运行方式..NET Native 并不是一帆风顺,它积极地从最终图像中消除类型以获得尽可能小的二进制文件.这在通常需要抖动才能正常工作的代码上具有锐利的边缘.特别是反射代码会流血,就像这段代码.

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.

您需要帮助并告诉工具链将 FontWeights 类型包含到最终图像中.打开项目的 Properties 节点并双击 Default.rd.xml.添加:

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.

这篇关于Type.GetProperties() 在 Release 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆