TypeInitializationException /强制所有静态c'tors在启动时运行 [英] TypeInitializationException / forcing all static c'tors to run on startup

查看:54
本文介绍了TypeInitializationException /强制所有静态c'tors在启动时运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚收到一个TypeInitializationException异常。为什么它是这个的常见原因?


InnerException是System.FormatException,虽然我没看到

任何要求格式化的东西。


我正试图调用一个类,强制它的静态c''tor到

run,将运行其单元测试。是否有一种强制方法可以强制所有

级别的静态c''tors在启动时运行?


Zytan

解决方案

是否有一种强制方法可以强制所有


class'的静态c' '在启动时运行?



没有简单的方法。你可能有一个用已知的定制属性标记的虚拟静态方法(NOP,没有

in-lining);使用反射

扫描所有类并调用?这将(根据C#规则;

CLR实际上支持更复杂的加载策略)强制

静态ctor(如果一个)执行...或者创建和实例如果默认ctors可用,那么这个类

...但没什么简单的。


当然,你可以在

是Program.cs / global.asax的顶部 - 运行时会更高效,但维护更高。


Marc


Zytan< zy ********** @ yahoo.comwrote:


我刚收到一个TypeInitializationException异常。为什么它是这个的常见原因?


InnerException是System.FormatException,虽然我没看到

任何要求格式化的东西。



我会仔细查看堆栈跟踪 - 它们应该显示涉及哪个
类。
< blockquote class =post_quotes>
我正在尝试调用类的使用,强制其静态c''tor运行,这将运行其单元测试。是否有一种强制方法可以强制所有

类的静态c''tors在启动时运行?



好​​吧,你可以使用Assembly.GetTypes()来获取

程序集中的所有类型,然后使用Type.TypeInitializer来获取一个ConstructorInfo,然后你可以调用



然而,这将强制任何已经运行

的类型初始值设定项为再次运行,这可能是危险的,取决于他们做什么!


作为一个副作用,我不确定使静态构造函数运行单元

测试是个好主意。我个人把它们放在一个单独的组件中,然后用bUnte来运行它们......


-

Jon Skeet - < ; sk *** @ pobox.com>
http://www.pobox .com / ~siget 博客: http://www.msmvps.com /jon.skeet

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


我刚收到一个TypeInitializationException异常。为什么它是


的常见原因是什么?


InnerException是System.FormatException,虽然我没有看到

要求格式化的东西。



我会仔细查看堆栈跟踪 - 它们应该显示涉及哪个
类。



查看InnerException的堆栈跟踪,我发现这是

问题:


string s = SomeMethod(); //可以返回null或"或任何字符串。

int x = int.Parse(" 0" + s);


如果s为空或,则0+ s应该起作用。或者非空字符串。

注释掉此代码删除了异常。这有什么问题?

那个代码?它在其他地方运行良好。


Zytan


I just got a TypeInitializationException exception. Why it is a
common cause for this?

The InnerException is System.FormatException, although I don''t see
anything requesting something to be formatted.

I''m trying to invoke the use of a class, to force its static c''tor to
run, which will run its unit test. Is there a good way to force all
class''s static c''tors to be run on startup?

Zytan

解决方案

Is there a good way to force all

class''s static c''tors to be run on startup?

No simple way. You could perhaps have a dummy static method (NOP, no
in-lining) marked with a known bespoke attribute; use reflection to
scan for this over all classes and invoke? This would (under C# rules;
the CLR actually supports more complex loading strategies) force the
static ctor (if one) to execute... Or create and instance of the class
if default ctors are available... but nothing simple.

Of course, you could simple list the necessary dummy static methods in
the top of Program.cs / global.asax - would be more efficient at
runtime, but higher maintenance.

Marc


Zytan <zy**********@yahoo.comwrote:

I just got a TypeInitializationException exception. Why it is a
common cause for this?

The InnerException is System.FormatException, although I don''t see
anything requesting something to be formatted.

I''d have a look at the stack traces carefully - they should show which
class is involved.

I''m trying to invoke the use of a class, to force its static c''tor to
run, which will run its unit test. Is there a good way to force all
class''s static c''tors to be run on startup?

Well, you can use Assembly.GetTypes() to get all the types in the
assembly, then use Type.TypeInitializer to get a ConstructorInfo which
you can then Invoke.

However, that will force any type initializers which have already run
to be run again, which could be dangerous depending on what they do!

As a side issue, I''m not sure that making static constructors run unit
tests is a good idea. I personally put them in a separate assembly and
run them with NUnit...

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


I just got a TypeInitializationException exception. Why it is a

common cause for this?

The InnerException is System.FormatException, although I don''t see
anything requesting something to be formatted.


I''d have a look at the stack traces carefully - they should show which
class is involved.

Looking in the stack trace of InnerException, I found this was the
problem:

string s = SomeMethod(); // can return null or "", or any string.
int x = int.Parse("0"+s);

Where "0"+s should work if s is null or "" or a non-empty string.
Commenting out this code removed the exception. What is wrong witt
that code? It runs fine elsewhere.

Zytan


这篇关于TypeInitializationException /强制所有静态c'tors在启动时运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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