在 UWP 中使用 Roslyn 脚本 [英] Using Roslyn scripting in UWP

查看:65
本文介绍了在 UWP 中使用 Roslyn 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码段.

var script = CSharpScript.Create("int x = 12;");
var result = await script.RunAsync();
var variable = result.GetVariable("x");
int value = (int)variable.Value;

它在第二行崩溃,内容如下:

It crashes on the second line with following:

无法加载文件或程序集System.Core,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089'.位于程序集的清单定义与程序集引用不匹配.(来自 HRESULT 的异常:0x80131040)

Could not load file or assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

这是一个 UWP 应用程序,我正在尝试使用 Microsoft.CodeAnalysis.CSharp.Scripting 库.我通过 NuGet 安装了这个包.应用编译和部署,但在此代码的第二行崩溃.

This is a UWP application and I am trying to use the Microsoft.CodeAnalysis.CSharp.Scripting library. I installed the package through NuGet. The app compiles and deploys, but crashes on the second line of this code.

显然,问题出在 System.Core DLL 中,该 DLL 的版本与我的应用程序 (4.6) 部署的版本和库使用的版本 (4.0) 之间存在冲突.有没有人有解决方案?有没有办法指定要使用的库版本或目标框架(即使我使用的是 UWP)?

Obviously, the problem is in the System.Core DLL which has a conflicting version between the one deployed with my app (4.6) and the one used by the library (4.0). Does anyone have a solution to this? Is there a way to specify a version of the library to be used or the target framework (even though I am using UWP)?

推荐答案

通过我这边的测试,我得到以下异常:

By testing on my side, I got the following exception:

System.NotSupportedException:AppX 不支持 Assembly.Load(byte[], ...)

System.NotSupportedException: Assembly.Load(byte[], ...) is not supported in AppX

和你的不一样.我使用了 Microsoft.CodeAnalysis.CSharp.Scripting 2.3.2

Not the same with yours. I used Microsoft.CodeAnalysis.CSharp.Scripting 2.3.2

实际上,脚本 API 不能在通用 Windows 应用程序和 .NET Native 中使用,因为应用程序模型不支持加载在运行时生成的代码.详情请参考这篇文章的支持的平台"部分.因此,目前您应该无法使用 UWP 的脚本 API.

Actually scripting APIs can't be used within Universal Windows Applications and .NET Native since the application model doesn't support loading code generated at runtime. Details please reference the "Supported Platforms" section of this article. So that currently you should not be able to use the scripting API for UWP.

这篇关于在 UWP 中使用 Roslyn 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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