代码之家  ›  专栏  ›  技术社区  ›  OGorbitz

仅在遍历代码时出现C#调试错误

  •  0
  • OGorbitz  · 技术社区  · 5 年前

    我的MonoGame应用程序在visual studio中遍历代码时输出错误,但在运行代码时没有。我很困惑,想知道我是否违反了一些规则。所以,这是有问题的代码:

        public class SpacialHash<T>
        {
            public Vector2 Size, CellSize;
            public HashCell<T>[,] Cells;
    
            public SpacialHash(Vector2 size, Vector2 cellsize)
            {
                Size = size;
                CellSize = cellsize;
                Vector2 hashSize = new Vector2((int)Math.Ceiling(size.X / cellsize.X), (int)Math.Ceiling(size.Y / cellsize.Y));
    
                Cells = new HashCell<T>[(int)hashSize.X, (int)hashSize.Y];
                for (int x = 0; x < hashSize.X; x++)
                {
                    for (int y = 0; y < hashSize.Y; y++)
                    {
                        Cells[x, y] = new HashCell<T>(this);
                    }
                }
            }
    
            public HashCell<T> AddMember(Vector2 position, T member)
            {
                Vector2 cpos = new Vector2((int)Math.Floor(position.X / CellSize.X), (int)Math.Floor(position.Y / CellSize.Y));
    
                //Breakpoint is here
                if (cpos.X < Cells.GetLength(0) && cpos.Y < Cells.GetLength(1) && cpos.X >= 0 && cpos.Y >= 0)
                {
                    //The following line of code causes the error while stepping through code
                    HashCell<T> cell = Cells[cpos.X, cpos.Y];
                    cell.AddMember(member);
                    return cell;
                }
                return null;
            }
        }
    

    该函数应该找到成员应该在其中的单元格(通过计算cpos),这似乎工作正常。程序检查cpos以确保它在界限内,然后声明单元格对象,将其分配给数组中cpos处存储的单元格的值。执行此操作的代码行会抛出以下错误:

    System.ExecutionEngineException
      HResult=0x80131506
      Source=<Cannot evaluate the exception source>
      StackTrace:
    <Cannot evaluate the exception stack trace>
    

    Error

    我甚至不知道从哪里开始调试这个。奇怪的是,运行代码时不会抛出错误,只是在遍历代码时才会抛出错误。那么,这种行为可能是由简单地使用具有泛型类的多维数组引起的吗?

    非常感谢。

    奥兹

    0 回复  |  直到 5 年前
        1
  •  1
  •   raddevus    5 年前

    我猜它绑定了错误版本的.net程序集,因为它甚至找不到异常类型(根据您显示的错误消息)。

    使用Fusion日志查看器(fuslogvw.exe)可以更好地确定是否存在故障。Fusion是的原始代号。NET发布之前。

    访问该应用程序的最简单方法是启动Visual Studio命令提示符(确保以管理员身份运行它,否则将无法进行更改),然后键入

    fuslogvw <ENTER>

    fuslogvw

    这是一个非常丑陋的实用程序,但它可以帮助你确定。NET绑定失败。

    您可以在以下网址查看有关如何设置它的更多信息: https://learn.microsoft.com/en-us/dotnet/framework/tools/fuslogvw-exe-assembly-binding-log-viewer

    单击[设置]并按如下方式设置:

    log all binds to disk

    现在,它将随时将值写入融合日志。NET绑定库。 回到你的程序,在调试模式下运行它,逐步执行代码,直到失败。

    当它失败时,返回Fusion日志查看器并单击查看日志按钮。
    希望你能看到的版本。它绑定到的.NET DLL。它可能与非调试运行时获得的版本不同。

    重要的

    当您全部完成或全部完成时,不要忘记将设置设置回[日志禁用]。NET程序将在整个系统范围内写入日志,从而降低计算机的运行速度。