静态构造函数 [英] Static constructors

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

问题描述

有人可以确认静态类构造函数在类的第一个

使用非静态构造函数时只被称为

,或者我做错了什么?

如果这是

确实是这种情况那么我会认为这是一个严重错误的语言

实现,

更不用说背面的痛苦了:(


另外,要问的是,方法Type.GetTypeFromCLSID是

记录在案吗? />
显然未实现?帮助提供以下内容:


参数

clsid

CLSID类型得到。

返回值

系统.__ ComObject,无论

CLSID是否有效。


然后继续提供一个使用这个无意义函数的例子。我

不介意

但是我已经使用了这个函数,当然,它不起作用。


Andy

解决方案

A J Le Couteur Bisson< no *** @ nowhere.com>写道:

有人可以确认静态类构造函数只在第一次使用类上的非静态构造函数时被调用,或者我做错了什么?


不,他们在创建第一个实例时或者在引用任何类型的静态字段时调用




也可以使用反射来调用它们。

如果确实如此,那么我认为这是语言实现中的一个严重错误,更不用说了背面的痛苦:(




我认为语义很好,而且远非严重的错误。

你期待所有以某种类似于某些C ++代码的方式加载

程序集时要执行的静态构造函数?如果是这样,那么

听起来像你的问题试图应用一个成语从一个

环境到另一个。


-

Jon Skeet - < sk *** @ pobox.com> ;
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



对CLR的说法有两个用于调用静态构造函数的策略。

1.默认情况下。一个类型的静态构造函数在第一个

访问类的成员(静态或实例)时调用。

2.当类型标记为*时beforefieldinit *属性。 CLR可以免费在第一次访问该类型的任何成员时调用类型initilizer或者

类型initilizer调用可以推迟到第一次访问静态

该类型的字段(非方法)。所以CLR有更多的自由。

这意味着无法保证当你开始使用类的实例创建和使用静态构造函数时,已经调用了
。它可能根本不会被调用。


C#为所有缺少显式的类设置* beforefieldinit *属性

静态构造函数如果静态构造函数是声明的
,则声明并且不会设置它。这样做无论在哪个静态字段上都有initlializer

表达式。


只要你使用类型initlizers,语义就可以了。构造函数)

来初始化类型。这不是一个好主意(我相信这是糟糕的设计)如果

你试着在静态构造函数中做更多的工作。


HTH

B \ rgds



" AJ Le Couteur Bisson" <无*** @ nowhere.com>在消息中写道

新闻:bn ********** @ newsfeed.th.ifl.net ...

有人可以确认一下静态类构造函数只是
在第一次使用类上的非静态构造函数时调用,或者我在做什么
错误?如果这确实如此,那么我会认为这是语言实施中的一个严重错误,
更不用说背后的痛苦了:(

另外是否要将Type.GetTypeFromCLSID方法明确记录为未实现?请帮助提供以下内容:

参数
clsid 要获取的类型的CLSID。
返回值
系统.__ ComObject,无论
CLSID是否有效。

然后继续提供示例使用这个毫无意义的功能。
我不介意
但是我使用了这个功能,当然,它不起作用。

Andy



100< 10*@100.com>写道:

只要你使用语义就行了类型initlizers(静态构造函数)
以初始化类型。


请注意,静态构造函数和类型

初始值设定项之间存在差异。静态构造函数是C#概念;一个类型的初始化程序

是一个.NET概念。


没有静态构造函数的类仍然可以有一个类型初始化程序

(由于静态字段被初始化等)。我怀疑你(100)

明白这一点,但是值得保持这两个术语不同。


-

Jon Skeet - < sk *** @ pobox.com>
http: //www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


Could someone please confirm that static class constructors are only called
at the first
use of a non-static constructor on the class, or am I doing something wrong?
If this is
indeed the case then I would consider this a serious error in the language
implementation,
not to mention a pain in the backside :(

Also, is it to much to ask that the method Type.GetTypeFromCLSID be
documented
clearly as unimplemented? The help gives the following:

Parameters
clsid
The CLSID of the type to get.
Return Value
System.__ComObject regardless of whether the
CLSID is valid.

and then goes on to provide an example of this pointless function in use. I
wouldn''t mind
but I have used this function and, of course, it doesn''t work.

Andy

解决方案

A J Le Couteur Bisson <no***@nowhere.com> wrote:

Could someone please confirm that static class constructors are only called
at the first use of a non-static constructor on the class, or am I doing
something wrong?
No, they''re called either when the first instance is created or when
any static field of the type is referenced.

They can also be called using reflection.
If this is indeed the case then I would consider this a serious error in
the language implementation, not to mention a pain in the backside :(



I think the semantics are fine, and far from a serious error at all.
Are you expecting all the static constructors to be executed when an
assembly loads, in a similar way to some C++ code? If so, then it
sounds like your problem is trying to apply an idiom from one
environment to another.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Hi A J,
Speeking of CLR there is two policies for calling static constructors.
1. By default. The static constructor of a type is called at the first
access to a member of the class(either static or instance).
2. When a type is marked with *beforefieldinit* attribute. CLR is free to
call the type initilizer at the first access to any member of the type or
the type initilizer call can be postponed until the first access to a static
field (not method) of the type. So CLR has more freedom.
Which means that there is no guarantee that when you start creating and
using instances of the class the static constructor is already called. It
might not get called at all.

C# sets *beforefieldinit* attribute for all classes that lack explicit
static constructor declaration and doesn''t set it if static constructor is
declared. It does that regardless of wheter static fields have initlializer
expressions on their declarations or not.

The semantics is OK as long as you use type initlizers (static constructors)
to initlize the type. It is not good idea (I believe it is bad design) if
you try to do more work then that in a static constructor.

HTH
B\rgds
100
"A J Le Couteur Bisson" <no***@nowhere.com> wrote in message
news:bn**********@newsfeed.th.ifl.net...

Could someone please confirm that static class constructors are only called at the first
use of a non-static constructor on the class, or am I doing something wrong? If this is
indeed the case then I would consider this a serious error in the language
implementation,
not to mention a pain in the backside :(

Also, is it to much to ask that the method Type.GetTypeFromCLSID be
documented
clearly as unimplemented? The help gives the following:

Parameters
clsid
The CLSID of the type to get.
Return Value
System.__ComObject regardless of whether the
CLSID is valid.

and then goes on to provide an example of this pointless function in use. I wouldn''t mind
but I have used this function and, of course, it doesn''t work.

Andy



100 <10*@100.com> wrote:

The semantics is OK as long as you use type initlizers (static constructors)
to initlize the type.



Note that there''s a difference between a static constructor and a type
initializer. A static constructor is a C# concept; a type initializer
is a .NET concept.

A class without a static constructor can still have a type initializer
(due to static fields being initialized, etc). I suspect you (100)
understand this, but it''s worth keeping the two terms distinct.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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

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