简体   繁体   English

在Windows Phone 8 C#app中不调用静态字段初始值设定项

[英]Static field initializer is not called in Windows Phone 8 C# app

I have a static class with a static field that is initialized in place: 我有一个静态类,其静态字段已初始化到位:

private static SomeType _instance = new SomeType();

This code is a part of a portable class library that is used on multiple platforms. 此代码是在多个平台上使用的可移植类库的一部分。 Everything works fine on desktop platforms, but when compiled for Windows Phone 8 the _instance is null. 在桌面平台上一切正常,但在为Windows Phone 8编译时,_instance为空。 But if I move the initialization to a default static constructor, the _instance is initialized properly. 但是,如果我将初始化移动到默认的静态构造函数,则_instance会正确初始化。

I tried to search for an explanation of this behavior but haven't found anything that would explain it. 我试图寻找这种行为的解释,但没有找到任何可以解释它的东西。

UPDATE . 更新 I spent some time trying to create a repeatable sequence of steps to reproduce the error, but at some point error no longer occurred even when I switched back to the original code. 我花了一些时间尝试创建一个可重复的步骤序列来重现错误,但在某些时候,即使我切换回原始代码,也不再发生错误。 I came to conclusion that this was a false alarm and the problem was apparently caused by something else. 我得出的结论是,这是一个误报,问题显然是由其他原因造成的。 I don't feel comfortable to leave it without explanation, but so far I have no grounds to believe that this has to do with static field initialization. 离开它没有解释我感到不舒服,但到目前为止,我没有理由相信这与静态字段初始化有关。

您需要确保没有其他任何内容正在更新静态值,因为静态属性将在创建应用程序域时初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM