当我使用自定义AppDomain和PermissionSet时,我在IronPython中遇到了一个奇怪的问题。出于某种原因,当PermissionSet不是Unrestricted并且我构建目标x64平台时,将双精度转换为浮点数无法正常工作。当我构建目标x86平台或使用PermissionState.Unrestricted时,问题就消失了。我也尝试从PermissionState.None开始,并将列出here的每个权限添加为Unrestricted,但它仍然无法正常工作。我正在使用IronPython 2.7.4 for .NET 4.0。下面是一个简单的程序,可以重现问题。知道这里发生了什么吗?
using IronPython.Hosting;
using System;
using System.Security;
using System.Security.Permissions;
namespace TestApp
{
public class Program
{
private const string Test = @"
from System import Console
num = float(10.0)
Console.WriteLine(num)
";
public static void Main(string[] args)
{
// Prints 10 always
var permissionSet1 = new PermissionSet(PermissionState.Unrestricted);
RunTest(permissionSet1);
// Prints 0 on x64 builds
var permissionSet2 = new PermissionSet(PermissionState.None);
permissionSet2.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
RunTest(permissionSet2);
Console.ReadKey();
}
private static void RunTest(PermissionSet permissionSet)
{
var appDomainSetup = new AppDomainSetup { ApplicationBase = "." };
var evidence = AppDomain.CurrentDomain.Evidence;
var appDomain = AppDomain.CreateDomain("ScriptingSandbox", evidence, appDomainSetup, permissionSet);
var scriptEngine = Python.CreateEngine(appDomain);
var scriptSource = scriptEngine.CreateScriptSourceFromString(Test);
scriptSource.Execute();
}
}
}