UWP应用的调试模式和发布模式

3
我正在尝试理解一个问题,当我在UWP应用程序上工作时遇到了这个问题。我已经解决了这个问题,但我仍然不清楚它的解释/原因。
我在我的代码库中使用XAMLBehaviours SDK中的"EventTriggerBehavior"。这个事件是用来检查GridView的"ClickedItem"的。
Microsoft.Xaml.Interactivity中的IAction.Execute方法将ClickedItem事件作为参数。
函数定义是object IAction.Execute(对象sender,对象parameter)
当我以调试模式运行应用程序时,这个函数正常工作,并且参数被正确分配。但当我将配置设置为发布时,我意识到我的Behaviors SDK没有正确工作。
这是先前的代码片段:
object IAction.Execute(object sender, object parameter)
    {

        object propertyValue = parameter;
        foreach (var propertyPathPart in propertyPathParts)
        {
            var propInfo = propertyValue.GetType().GetTypeInfo().GetDeclaredProperty(propertyPathPart);
            if (propInfo != null)
                propertyValue = propInfo.GetValue(propertyValue);
        }
    }

进一步调查后,我意识到propertyValue未被正确初始化。因此,为了解决这个问题,我对参数进行了类型转换。
object propertyValue = parameter as ItemClickEventArgs;

现在,在启用代码优化的情况下,所有内容都在发布模式下正常工作。我认为,在启用.NET Native工具链进行编译时,System.reflection在发布模式下无法正常工作。当我进行隐式转换时,就没有问题了。根据这个视频https://channel9.msdn.com/Shows/Going+Deep/Inside-NET-Native,反射仍然可以工作,但是我必须为Behaviors SDK进行转换。我想了解更详细的信息并正确理解这个问题。
1个回答

3
在你的uwp项目中,你可以找到一个名为Default.rd.xml的文件(在Properties文件夹内)。这是一个配置文件,指定当启用.net native时,指定的程序元素是否可用于反射。
在你的情况下,你可以添加以下声明来添加ItemClickEventArgs类型。如果需要,还有一些选项可以声明命名空间而不是类型。
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <!--
      An Assembly element with Name="*Application*" applies to all assemblies in
      the application package. The asterisks are not wildcards.
    -->
    <Assembly Name="*Application*" Dynamic="Required All"/>

    <!-- Add your application specific runtime directives here. -->
    <Type Name="Windows.UI.Xaml.Controls.ItemClickEventArgs" Browse="Required Public"/>

  </Application>
</Directives>

您可以查看以下链接获取更多信息:

反射和.NET Native

NET Native深入解析:帮助!我遇到了MissingMetadataException问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接