在基类中调用静态构造函数 [英] Invoking static constructors in base classes

查看:68
本文介绍了在基类中调用静态构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个带有静态构造函数和一些抽象方法的基类。

派生类实现这些方法。从网上的文章来看,似乎没有保证即使派生类的一个对象是基类,也会调用基类

的静态构造函数。创建。这是

是否正确?有没有办法确保在构造派生类实例之前调用基类的静态构造函数是否为



谢谢

Hemanth

Hi,

I have a base class with a static constructor and some abstract methods.
Derived classes implement these methods. From articles on the web, it appears
that there is no guarentee that this static constructor of the base class
would be invoked even if a an object of the derived class is created. Is this
correct ? Is there any way to ensure the base class''s static constructor is
invoked before the derived class instance is constructed ?

Thanks
Hemanth

推荐答案

Hemanth写道:
Hemanth wrote:
我有一个带有静态构造函数和一些抽象方法的基类。
派生类实现这些方法。来自网络上的文章,


什么文章?

看来
没有保证这个基类的静态构造函数。这个
是否正确?
I have a base class with a static constructor and some abstract methods.
Derived classes implement these methods. From articles on the web,
What articles?
it appears
that there is no guarentee that this static constructor of the base class
would be invoked even if a an object of the derived class is created. Is this
correct ?




很可能不是。加载类时会调用静态构造函数。



Most likely not. The static constructor is invoked when the class is loaded.


阅读Jon关于你的观点的文章
http://www.yoda.arachsys.com/csharp/constructors.html
Read Jon''s article about your point
http://www.yoda.arachsys.com/csharp/constructors.html
我有一个带有静态构造函数和一些抽象方法的基类。
派生类实现了这些方法。从Web上的文章来看,似乎没有保证即使创建派生类的对象也会调用基类的静态构造函数。这个
是否正确?有没有办法确保在构造派生类实例之前调用基类的静态构造函数?
I have a base class with a static constructor and some abstract methods.
Derived classes implement these methods. From articles on the web, it appears
that there is no guarentee that this static constructor of the base class
would be invoked even if a an object of the derived class is created. Is this
correct ? Is there any way to ensure the base class''s static constructor is
invoked before the derived class instance is constructed ?




-

WBR,

Michael Nemtsev ::博客: http: //spaces.msn.com/laflour


有时一个人仍然忠实于一个事业只因为它的对手没有

停止平淡无奇。 (c)Friedrich Nietzsche



--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


实际上有两种类型的调用类型初始化器:

1.在此之前的某个时间,但是之后不要再调用任何方法或访问



类型的任何

2.之前的任何时间,但不能更晚访问

类型的任何静态字段


如何调用类型初始化程序取决于类是否标记为

with * beforefieldinit * - 如果没有标记则为1,如果标记则为2。


我相信如果类型没有标记,那么这将保证类型

初始化程序将只要

实例构造函数属于调用实例方法的类别,就可以在实例化类型之前调用它。


C#不会提供一种直接的方式来指定一个类是否需要设置

as * beforefieldinit *或者不是,但C#规范声明如果一个类有静态

construtor它应该被标记为* beforefieldinit *。


因此,如果想要保证在实例化类之前静态字段将被初始化

,如果构造函数为空,则应该声明静态构造函数




" G?ran Andersson" <顾*** @ guffa.com>在消息中写道

新闻:el **************** @ TK2MSFTNGP04.phx.gbl ...
Actually there is two types of invoking a type initializer:
1. At some time before, but not later of calling of any method or access any
filed of the type
or
2. At any time before, but not later of accessing any static field of the
type

How the type initializer is called depends on whether the class is marked
with *beforefieldinit* - 1 if not marked and 2 if marked.

I believe if the type is not marked then this will guarantee that the type
initializer will be called before instatiating the type as long as the
instance constructor falls in the category of "calling an instance method".

C# doesn''t provide a straight way to specify whether a class needs to be set
as *beforefieldinit* or not, but C# specs state that if a class has a static
construtor it should neve be marked as *beforefieldinit*.

So, if one want to guarantee that the static fields will be initialized
before the class is instantiated, one should declare static constructor even
if the constructor is empty.

"G?ran Andersson" <gu***@guffa.com> wrote in message
news:el****************@TK2MSFTNGP04.phx.gbl...
Hemanth写道:
Hemanth wrote:
我有一个带有静态构造函数和一些抽象方法的基类。
派生类实现了这些方法。来自网上的文章,
I have a base class with a static constructor and some abstract methods.
Derived classes implement these methods. From articles on the web,



什么文章?



What articles?

似乎没有保证这个静态构造函数的
即使创建了派生类的对象,也会调用基类。这是正确的吗?
it appears that there is no guarentee that this static constructor of the
base class would be invoked even if a an object of the derived class is
created. Is this correct ?



很可能不是。在加载类时调用静态构造函数。



Most likely not. The static constructor is invoked when the class is
loaded.



这篇关于在基类中调用静态构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆