问题标签 [static-constructor]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c# - MethodBase.IsConstructor 不能按照指定的静态构造函数工作
只是一个简单的观察。该属性MethodBase.IsConstructor
不适用于static
构造函数,并且文档没有提到这一事实(引用:“如果此方法是由对象表示的构造函数,则为trueConstructorInfo
”)。
样本:
问:为什么?错误或不完整的规范?
c# - 为什么不调用我的基类中的静态构造函数?
假设我有 2 个课程:
我希望在调用之后Bar.DoSomething()
(假设这是我第一次访问 Bar 类),事件的顺序将是:
- Foo 的静态构造函数(再次假设第一次访问)> 打印
4
- Bar 的静态构造函数 > 打印
2
- 执行
DoSomething
在底线,我希望42
被打印出来。
经过测试,似乎只有2
正在打印。
这甚至不是一个答案。
你能解释一下这种行为吗?
c# - How to discover all classes including subclasses with attribute attached at runtime?
I have an attribute that I use to mark certain classes in my solution. I have to check whether this attribute is on objects that are moving around. This check (type.IsDefined(typeof(XmlImmutableAttribute), true);
) is being done so often that is is becoming a burden and hurting performance. I have dealt with a similar problem before by finding all the types with the attribute attached and storing them in a HashSet, and checking set.Contains(type);
(see my answer here). This is the code I currently have:
The issue is that m_XmlImmutableAttributeTypes
only becomes initialized to contain the types that have the attributes directly attached, and non of the types that have been sub classed from the type attached. I assume this is an issue with the check being done in the static constructor of the attribute itself, because when I check type.IsDefined(typeof(XmlImmutableAttribute), false)
on a subclass after the static initialization, it returns true. How can I maintain this pattern of predetermining types for increased efficiency, while also having it detect the sub classes with the attribute attached?
c# - 将参数传递给单例类/静态构造函数
我有一个Listener
类来监听特定的 HTTP 端口。我制作了Listener
单例(因为总会有一个侦听器对象在特定端口上侦听)。为此,我制作了默认Listener()
构造函数private
。制作Listener
实例(在Listener
类内部)static
并让static Listener
构造函数初始化单例Listener
对象。也有static Listener getListener()
which 返回单例Listener
实例。
我在此端口上接收到一个 SOAP,我的侦听器回调方法正在处理该 SOAP。
现在我想将最后一次通知的日期时间推送到我的 UI。
所以我想的是,在初始化时Listener
我会传递一个委托,一个对它的引用将被存储在里面Listener
,然后每次我收到通知时,在监听器回调中我会调用那个委托并传递它当前时间(new DateTime()
),这会做随着时间的推移打印消息的必要任务。我还想过让委托参数可选,这样如果委托是null
它就不会在通知时被调用。但是,由于我将构造函数设为私有,因此我无法弄清楚如何将委托传递给它。
总结和概括整个问题:如何将参数(作为实例成员存储)传递给单例类?
我应该将委托传递给getListener()
吗?并在其中每次检查委托是否为空,如果不是,设置它?但是,如果我通过调用访问这个单例对象太多次Listener.getListener()
,每次它都会不必要地检查委托是null
。事实上,这就是为什么我将初始化转移Listener
到静态构造函数只是为了避免检查是否Listener
在每次getListener()
调用时都被初始化。
我应该怎么做才能将参数传递给单例类?或者这是getListener()
不可避免的检查?
注意:我不知道,但这个问题可能归结为我们如何将参数传递给静态构造函数,因为我们需要在第一次使用类时初始化事物,并且我们必须使用委托/其他编程构造来初始化它(因此我们不能从静态构造函数中的某个配置文件加载初始化参数)。
d - 在静态构造函数中退出 D 程序
我有一个像这样的静态构造函数:
打开文件失败时如何停止程序执行?
c# - 创建类的新实例时出现 NullReferenceException
据我所知,错误发生在以下行之一:
SomeClass 基本上是这样的:
在将本准则翻译成更笼统的术语时,我误读了它。我已经纠正了。这还有意义吗?
我对静态构造函数的经验很少,但我的直觉告诉我问题可能出在某个地方。
c# - 静态初始化器/构造器排序
我今天遇到了一个有趣的错误,下面的代码会在某些机器上的注释行上崩溃,而在其他机器上则不会。该问题似乎与静态构造函数、静态初始化程序和继承的顺序有关。
解决方法是将#region 中的代码移到另一个类中,但我仍然不明白实际发生了什么,以及为什么它似乎只发生在某些机器上。
我看过这两篇文章:http: //csharpindepth.com/Articles/General/Singleton.aspx
http://csharpindepth.com/Articles/General/BeforeFieldInit.aspx
这揭示了一些见解,但都没有涉及继承如何影响事物。
c# - 我错了还是 Roslyn REPL 保护过度了?
我正在尝试像 LinqPad 一样使用 Roslyn,但我正在使用完全有效的 C# 代码片段并且被告知它们是无效的。考虑这种沼泽标准实用方法。
Roslyn 声称我调用 out 参数是无效的ToUnderlying
。
在你问我为什么不使用静态构造函数之前,我想确保我beforefieldinit
在我的类上保留了类属性。否则,我将支付每次访问该方法时初始化它的费用。在 C# 中,这被认为是有效的,但 Roslyn 告诉我(6,76): error CS0199: A static readonly field cannot be passed ref or out (except in a static constructor)
static-constructor - 如何在 C# 中调用静态类的析构函数?
嗨,我是 OOPS 的初学者,请帮助我理解这一点
如何调用静态类的贬损者?或者如何为静态类、方法和对象释放内存?
c# - 使用 new 进行静态变量初始化会带来代码风险
我正在处理一些类似这样的代码:
Someclass 包含所需的构造函数。此代码编译良好,没有任何警告。但是我在系统中遇到了代码危险:
“已从静态构造函数和/或静态初始化程序调用了 Someclass ctor”
该代码危害系统的一部分,只是为了通过警告系统中可能存在的缺陷或系统是否因此而进入不良状态来使其变得更好。我在网上某处读到,如果静态构造函数/初始化程序等待线程完成,它们可能会在 c# 中陷入死锁。这和这个有关系吗?
我需要摆脱这个警告我该怎么做。我不能使成员成为非静态成员,因为它被静态函数使用。在这种情况下我该怎么办,需要帮助。