惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - zencorn

使用 WinSCP 下载 FTP 文件并用 计划任务 复制到 远程 共享目录 【翻译】什么是 eCPM & RPM 与其计算公式 PMP Fundamentals T-SQL 总结 MySQL 存储配置 MS-TEST 批处理执行测试时的资源文件目录问题 测试换行显示 如何制作简单的 3D 打印模型 搬家Testing. Test TFS 自动同步Server 端文件的批处理命令 Perl初级教程 (5) 遍历文件夹内指定扩展名文件,查找匹配关键字的输出。 最方便的批处理延时方法 SQLServer2005 remove log file. Disable Windows server 2003 Security Warning. Perl Scalar Perl 基于 Windows 环境 搭建 Package you execution files with Iexpress.exe Automation testing framework for RFT execution with STAF+STAX . [Session-1]
.NET Core 6 类的私有静态变量关联异常
zencorn · 2024-01-11 · via 博客园 - zencorn

因为,在.NET Core 6(以及其他.NET版本)中,类的私有静态变量在类第一次被任何方式引用时被初始化。

所以,

1: 在Debug时,如果跟踪某个方法是查看类的私有静态变量被访问前,无法单步跟踪查看这些静态变量的赋值。

2: 如果静态变量的初始化涉及到复杂的计算或者可能抛出异常,那么这个初始化过程会被延迟到静态变量被实际用到的时候,会导致在查看当前静态变量时,由于其它静态变量在后台有调用赋值时抛出的异常,但却与当前调试的静态变量无关。这种情况下,静态构造函数(如果存在)会在静态变量被用到之前调用,用于执行初始化过程。

Sample

public class MyClass
{
private static int myVariable = OtherMethods(); // 方法中可能报错,但没有访问myVariable时没有触发该异常

private static int myRequest = “First Request”;
}

在这个例子中,myVariable将在MyClass第一次被引用时初始化,无论是创建实例、访问静态成员还是其他方式。

如果此时在其他方法中首先访问myVariable ,则可能会触发访问所有静态变量,从而触发 OtherMethods中的异常,但却与当前myVariable无关。

所以最好通过静态类,来实现对需复杂计算赋值的静态变量赋值。

static MyClass()

    {

        myVariable =  OtherMethods();

    }

- Make people around you successful is the biggest contribution to ourselves. -