VS2022中静态构造函数代码执行但断点未命中的问题问询
Visual Studio 2022调试静态构造函数断点未命中的异常情况
问题场景与表现
在Visual Studio 2022专业版17.7.4中,可复现如下调试异常:
- 类库项目包含带静态构造函数的
Customer类,测试项目对应有单元测试方法TestMethod1 - 同时在
Customer的静态构造函数、TestMethod1的第一行设置断点 - 调试运行
TestMethod1时,测试方法的断点正常命中,但多次按F11或继续运行,静态构造函数的断点始终无法触发 - 静态构造函数实际已执行(可通过
TestMethod1中获取到name变量值为"John"验证) - 移除
TestMethod1中的断点后,静态构造函数的断点可正常命中 - 在JetBrains Rider中测试无此问题,无论测试方法是否有断点,静态构造函数断点都会正常触发,推测为VS调试器自身问题
相关代码
类库项目代码
namespace SomeLibrary { public class Customer { public static readonly Customer SomeCustomer; public static string name; private Customer() { } static Customer() { SomeCustomer = new Customer(); name = "John"; } public static void SomeMethod() { } } }
测试项目代码
using SomeLibrary; namespace TestProject1 { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { Customer.SomeCustomer.ToString(); Customer.SomeMethod(); string name = Customer.name; } } }
内容的提问来源于stack exchange,提问作者Tony_Henrich
相关产品推荐
相关产品推荐

