运行时无法调用其默认构造函数,为什么?

编程入门 行业动态 更新时间:2024-10-28 14:35:11
本文介绍了运行时无法调用其默认构造函数,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个与.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.

更多推荐

运行时无法调用其默认构造函数,为什么?

本文发布于:2023-11-26 22:19:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635430.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!