运行时无法调用其默认构造函数,为什么? [英] Runtime is unable to call its default constructor , why ???

查看:64
本文介绍了运行时无法调用其默认构造函数,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与.Net运行时有关的问题,无法调用Type的默认构造函数.但是我不明白原因,为什么它不能调用默认构造函数?

以下是Jeffrey Richter在MSDN文章之一中的解释.

在极少数情况下,运行时必须初始化值类型并且无法调用其默认构造函数.

例如,当非托管线程首次执行托管代码时必须分配并初始化线程局部值类型时,可能会发生这种情况.在这种情况下,运行时无法调用类型的构造函数,但仍可确保所有成员都已初始化 为零或null.因此,建议您不要在值类型上定义无参数的构造函数.实际上,C#编译器(和其他编译器)认为这是一个错误,不会编译代码."

有人可以解释为什么Runtime无法调用类型的默认构造函数吗?关于上述示例?

谢谢

Venkataramana M

 

 

 

解决方案

您是否确实存在此问题,或者只是想知道如何从非托管状态初始化.NET框架,线?为什么你甚至想知道这个?在极少数情况下,您甚至还会涉足此问题.


I've a question related to .Net Runtime can't call the Type's default consturctor. But i couldn't understand the reason , why it can't call default constructor ??

The following is an explanation by Jeffrey Richter in one of the MSDN Articles.

"There are rare situations when the runtime must initialize a value type and is unable to call its default constructor.

For example, this can happen when a thread local value type must be allocated and initialized when an unmanaged thread first executes managed code. In this situation, the runtime can't call the type's constructor but still ensures that all members are initialized to zero or null. For this reason, it is recommended that you don't define a parameterless constructor on a value type. In fact, the C# compiler (and others) consider this an error and won't compile the code."

Can somebody explain why can't Runtime call on type's default constructor  with respect to the above example ?

Thanks,

Venkataramana M

 

 

 

解决方案

Do you actually have this issue, or do you just want to know how the .NET framework is initialized from an unmanaged thread? Why would you even want to know this? There are only very rare situations where you would even walk into this issue.


这篇关于运行时无法调用其默认构造函数,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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