Unity编译器不完全支持命名参数吗?

本文关键字:参数 编译器 不完全 支持 Unity | 更新日期: 2023-09-27 18:01:15

下面的代码在Visual Studio中可以正常编译,但是在Unity (4.6.x)中会产生编译错误:

public class ErrorTest 
{
    void DoSomething(bool a = true, bool f = true) {  }
    void DoSomething(int b, bool f = true) {  }
    public void SomeMethod()
    {
       DoSomething(f: false);
    }
}

对DoSomething的调用使编译器阻塞。问题是为什么没有歧义。


Unity阻塞时控制台输出如下:

Internal compiler error. See the console log for more information. output was:
Unhandled Exception: Mono.CSharp.InternalErrorException: Internal error
  at Mono.CSharp.MethodGroupExpr.IsApplicable (Mono.CSharp.ResolveContext ec, Mono.CSharp.Arguments& arguments, Int32 arg_count, System.Reflection.MethodBase& method, System.Boolean& params_expanded_form) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.MethodGroupExpr.OverloadResolve (Mono.CSharp.ResolveContext ec, Mono.CSharp.Arguments& Arguments, Boolean may_fail, Location loc) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Invocation.DoResolveOverload (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Invocation.DoResolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec, ResolveFlags flags) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.ExpressionStatement.ResolveStatement (Mono.CSharp.BlockContext ec) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.StatementExpression.Resolve (Mono.CSharp.BlockContext ec) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Block.Resolve (Mono.CSharp.BlockContext ec) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.ToplevelBlock.Resolve (Mono.CSharp.FlowBranching parent, Mono.CSharp.BlockContext rc, Mono.CSharp.ParametersCompiled ip, IMethodData md) [0x00000] in <filename unknown>:0 

Unity编译器不完全支持命名参数吗?

显然有一个错误,将名称空间和默认参数的使用结合起来会导致这样的内部错误。据说这个bug在4.5版本中修复了,但是从评论来看,这个bug之前就已经修复过了。您可以尝试消除类中的名称空间声明作为实验,看看您是否遇到了该错误。