PlatformNotSupportedException 在 WP 中使用 Dapper 时抛出

JiB*_*evé 6 c# dapper uwp

DEBUG完美地执行 UWP 应用程序。

使用在RELEASE崩溃中编译的完全相同的代码与此错误消息

System.PlatformNotSupportedException: 
    'Dynamic code generation is not supported on this platform.'
Run Code Online (Sandbox Code Playgroud)

执行此代码时(它使用Dapper 1.5.1and System.Data.SQLite 1.0.109.2

using (var c = NewConnection())
{
    var sql = @"
        update settings
        set
            ""value"" = @SetDate
        where ""key"" = 'week_date'";
    c.Execute(sql, new { SetDate = date }); //<= throws PlatformNotSupportedException 
                                            // only on RELEASE not in DEBUG
}
Run Code Online (Sandbox Code Playgroud)

该应用程序是 UWP 配置如下。此外,故障代码是一个.NET Standard 2.0 Class Library

在此处输入图片说明

为什么它RELEASE只会崩溃以及如何修复它?

Mar*_*ell 5

Dapper 非常深入地基于运行时 IL 生成,其方式基本上不可能改变。运行时 IL 生成从根本上与 UWP 不兼容。

没有简单的方法来完成这项工作。

所以:要做到这一点,你需要使用一些类似 dapper-like-but-not-dapper 的东西,以及两种替代实现之一:

  • 基于反射的绑定(相对较慢,取决于您正在执行的数据访问量)
  • 缺少部分的编译时代码生成,大概使用某种 roslyn 分析和部分类生成

也许现在,更实用的方法是:在这种情况下不要使用 dapper。