如何在 Unity 播放器或内置游戏中使用 Roslyn 编译器来处理可能包含 lambda 表达式的表达式?

Chr*_*ski 5 c# lambda unity-game-engine roslyn

目前,我正在尝试在我的 Unity 项目中使用 Roslyn C# 编译器在运行时编译一些表达式,以便我可以将它们作为委托执行,而不是一遍又一遍地重新评估表达式文本。显然,出于性能原因,我需要这个。

在与 Unity 的尴尬行为斗争了几个月之后,我终于成功实现了大约 90% 的业务逻辑,为 Unity 安装了 NuGet,包含DynamicExpresso(几乎可以正常工作,但不支持我需要的 lambda 表达式)并尝试其他 NuGet包喜欢Microsoft.CodeDom.Providers.DotNetCompilerPlatformMicrosoft.CodeAnalysis作为表示这里

问题是:

  • 我从表单的一些简单的通用功能开始f(x, y, z) = x * y + z,但是这太不灵活了,很快就被放弃了。
  • DynamicExpresso有效,但不支持 lambda 表达式。似乎可以重写代码以递归处理 lambda 表达式,但我没有足够的时间了。
  • Microsoft.CodeAnalysis 打破了解决方案,因为它与其他库有冲突,我很难恢复它。
  • Microsoft.CodeDom.Providers.DotNetCompilerPlatform至少不会破坏解决方案,我可以在单独的测试项目中使用它而不会出现任何问题(我之前已经完成了数十次),但是在 Unity(编辑器和播放器)中我得到了一个System.Security.SecurityException(无法模拟令牌) 当我尝试使用CSharpCodeProvider.CompileAssemblyFromSource(CompilerParameters, params string[]).

Unity 中似乎有一些 Roslyn 支持,但我无法使其工作。我的代码是这样的:

using UnityEngine;
using CSCodeProvider = Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider;

string code = "public static class CompiledExpressions { public static int f(int x) => 2*x; }";

CompilerResults result = null;
Assembly asm = null;

var types = new List<Type>()
{
    typeof(int),
    typeof(Mathf),
    typeof(GameObjectExtensions) // a helper class with extension functions for unimplemented trivial tasks with GameObjects
};

try
{
    CSCodeProvider csprovider = new CSCodeProvider(new OurCompilerSettings()); // OurCompilerSettings just helps locate csc.exe
    CompilerParameters cparams = new CompilerParameters();
    cparams.GenerateExecutable = false;
    cparams.GenerateInMemory = true;
    cparams.ReferencedAssemblies.Clear();
    cparams.ReferencedAssemblies.AddRange(types.Select(t => t.Assembly.Location).Distinct().ToArray());

    result = csprovider.CompileAssemblyFromSource(cparams, new string[] { code }); // here comes the exception
    if (!result.Errors.HasErrors)
        asm = result.CompiledAssembly;
}
catch (Exception ex)
{
    Debug.LogError(ex);
}

if (asm != null)
    // do stuff
Run Code Online (Sandbox Code Playgroud)

错误是:

System.Security.SecurityException: Couldn't impersonate token.
  at System.Security.Principal.WindowsImpersonationContext..ctor (System.IntPtr token) [0x0001a] in <437ba245d8404784b9fbab9b439ac908>:0 
  at System.Security.Principal.WindowsIdentity.Impersonate (System.IntPtr userToken) [0x00000] in <437ba245d8404784b9fbab9b439ac908>:0 
  at Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Compiler.RevertImpersonation () [0x00006] in <fefe506461e04012a8bd9f406d3641b2>:0 
  at Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Compiler.FromSourceBatch (System.CodeDom.Compiler.CompilerParameters options, System.String[] sources) [0x000c1] in <fefe506461e04012a8bd9f406d3641b2>:0 
  at Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Compiler.CompileAssemblyFromSourceBatch (System.CodeDom.Compiler.CompilerParameters options, System.String[] sources) [0x0001d] in <fefe506461e04012a8bd9f406d3641b2>:0 
  at System.CodeDom.Compiler.CodeDomProvider.CompileAssemblyFromSource (System.CodeDom.Compiler.CompilerParameters options, System.String[] sources) [0x00006] in <ae22a4e8f83c41d69684ae7f557133d9>:0 
  at MyProject.RoslynHelper.Compile () [0x0017e] in C:\Users\myname\source\repos\MyProject\MyProject\Assets\scripts\RoslynHelper.cs:137 
0x00007FF77A09D52C (Unity) StackWalker::GetCurrentCallstack
0x00007FF77A0A0991 (Unity) StackWalker::ShowCallstack
0x00007FF7787E54D5 (Unity) GetStacktrace
0x00007FF77AD020CE (Unity) DebugStringToFile
0x00007FF77A1051E5 (Unity) DebugLogHandler_CUSTOM_Internal_Log
0x000001D42D6000EB (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,UnityEngine.LogOption,string,UnityEngine.Object)
0x000001D4534AFF8B (Mono JIT Code) UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
0x000001D42D5F090E (Mono JIT Code) UnityEngine.Logger:Log (UnityEngine.LogType,object)
0x000001D42D5F055A (Mono JIT Code) UnityEngine.Debug:LogError (object)
0x000001D42D5E8B33 (Mono JIT Code) [RoslynHelper.cs:143] MyProject.RoslynHelper:Compile () 
0x000001D42D5910BB (Mono JIT Code) [Game.cs:107] MyProject.Game:.ctor () 
0x000001D45359FDCB (Mono JIT Code) [Game.cs:51] MyProject.Game:get_Instance () 
0x000001D42D613BDB (Mono JIT Code) [InteractionReceiver.cs:312] MyProject.InteractionReceiver:Update () 
0x000001D45317AC38 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
0x00007FF82844CBB0 (mono-2.0-bdwgc) [mini-runtime.c:2809] mono_jit_runtime_invoke 
0x00007FF8283D2122 (mono-2.0-bdwgc) [object.c:2921] do_runtime_invoke 
0x00007FF8283DB11F (mono-2.0-bdwgc) [object.c:2968] mono_runtime_invoke 
0x00007FF77A01EFEE (Unity) scripting_method_invoke
0x00007FF77A018D3D (Unity) ScriptingInvocation::Invoke
0x00007FF779FE2965 (Unity) MonoBehaviour::CallMethodIfAvailable
0x00007FF779FE2A76 (Unity) MonoBehaviour::CallUpdateMethod
0x00007FF779690D38 (Unity) BaseBehaviourManager::CommonUpdate<BehaviourManager>
0x00007FF779699EB4 (Unity) BehaviourManager::Update
0x00007FF779AC7FE3 (Unity) `InitPlayerLoopCallbacks'::`2'::UpdateScriptRunBehaviourUpdateRegistrator::Forward
0x00007FF779AB13D8 (Unity) ExecutePlayerLoop
0x00007FF779AB14AD (Unity) ExecutePlayerLoop
0x00007FF779AB66F4 (Unity) PlayerLoop
0x00007FF777EC60FB (Unity) PlayerLoopController::UpdateScene
0x00007FF777EC4038 (Unity) Application::TickTimer
0x00007FF778809740 (Unity) MainMessageLoop
0x00007FF7788134DA (Unity) WinMain
0x00007FF77B79B012 (Unity) __scrt_common_main_seh
0x00007FF864D44034 (KERNEL32) BaseThreadInitThunk
0x00007FF867903691 (ntdll) RtlUserThreadStart
Run Code Online (Sandbox Code Playgroud)

有没有人知道如何在 Unity 中使用运行时编译?