如何使用monocsharp . evaluator编译委托

本文关键字:编译 evaluator 何使用 monocsharp | 更新日期: 2023-09-27 18:15:03

我正试图编译一大块C代码,并得到一个委托,我可以用它来调用代码以后。到目前为止,我已经尝试了

eval.Compile("delegate () {}");

eval.Evaluate("delegate () {}");

两种情况给我相同的错误:

FatalException: Cannot convert `anonymous method' to non-delegate type `object'
Mono.CSharp.Report.Error (Int32 code, Location loc, System.String error)
Mono.CSharp.Report.Error (Int32 code, Location loc, System.String format, System.String arg1, System.String arg2)
Mono.CSharp.AnonymousMethodExpression.CompatibleChecks (Mono.CSharp.ResolveContext ec, Mono.CSharp.TypeSpec delegate_type)
Mono.CSharp.AnonymousMethodExpression.Compatible (Mono.CSharp.ResolveContext ec, Mono.CSharp.TypeSpec type)
Mono.CSharp.Convert.ImplicitConversionStandard (Mono.CSharp.ResolveContext ec, Mono.CSharp.Expression expr, Mono.CSharp.TypeSpec target_type, Location loc, Boolean explicit_cast)
Mono.CSharp.Convert.ImplicitConversionStandard (Mono.CSharp.ResolveContext ec, Mono.CSharp.Expression expr, Mono.CSharp.TypeSpec target_type, Location loc)
Mono.CSharp.Convert.ImplicitConversion (Mono.CSharp.ResolveContext ec, Mono.CSharp.Expression expr, Mono.CSharp.TypeSpec target_type, Location loc)
Mono.CSharp.Convert.ImplicitConversionRequired (Mono.CSharp.ResolveContext ec, Mono.CSharp.Expression source, Mono.CSharp.TypeSpec target_type, Location loc)
Mono.CSharp.Assign.ResolveConversions (Mono.CSharp.ResolveContext ec)
Mono.CSharp.Assign.DoResolve (Mono.CSharp.ResolveContext ec)
Mono.CSharp.SimpleAssign.DoResolve (Mono.CSharp.ResolveContext ec)
Mono.CSharp.OptionalAssign.DoResolve (Mono.CSharp.ResolveContext ec)
Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec, ResolveFlags flags)
Rethrow as InternalErrorException: (1,1): InteractiveHost
Mono.CSharp.Method.Emit ()
Mono.CSharp.TypeDefinition.Emit ()
Mono.CSharp.ClassOrStruct.Emit ()
Mono.CSharp.Class.Emit ()
Mono.CSharp.TypeDefinition.EmitContainer ()
Mono.CSharp.Evaluator.CompileBlock (Mono.CSharp.Class host, Mono.CSharp.Undo undo, Mono.CSharp.Report Report)
Mono.CSharp.Evaluator.Compile (System.String input, Mono.CSharp.CompiledMethod& compiled)
Mono.CSharp.Evaluator.Compile (System.String input)
DynamicMonoBehaviour.SetDynamicMethod (System.String methodname, System.String code, System.Type returnType) (at Assets/Kelvin4/DynamicMonoBehaviour.cs:53)
DynamicMonoBehaviour.Awake () (at Assets/Kelvin4/DynamicMonoBehaviour.cs:37)

如何使用monocsharp . evaluator编译委托

匿名方法和lambda表达式都是无类型表达式,不能单独存在。

您需要显式地将其转换为委托类型,以便它成为对象:

new Action(delegate { ... })