而不调用构造函数创建对象的实例? [英] Create object instance without invoking constructor?

查看:247
本文介绍了而不调用构造函数创建对象的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,有没有一种方法来实例化一个类的实例,而不必调用它的构造函数?

假设类是公众和第三方库的定义和构造函数是内部的。其原因我想这样做是复杂,但将是有益的知道,如果使用某种C#两轮牛车的可能。

Assume the class is public and is defined in a 3rd party library and the constructor is internal. The reasons I want to do this are complicated but it would be helpful to know if it's possible using some kind of C# hackery.

请注意:我特别的不想调用任何构造这样使用反射来访问内部构造是不是一种选择

NOTE: I specifically do not want to call any constructor so using reflection to access the internal constructor is not an option.

推荐答案

我还没有试过,但有一个方法叫<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatterservices.getuninitializedobject.aspx\">GetUninitializedObject这是反序列化过程中使用。

I have not tried this, but there is a method called GetUninitializedObject that is used during deserialization.

从MSDN备注说:

由于该对象的新实例
  初始化为零和无
  构造函数运行时,对象可能
  不重present被视为一个国家
  由该对象是有效的。

Because the new instance of the object is initialized to zero and no constructors are run, the object might not represent a state that is regarded as valid by that object.

这篇关于而不调用构造函数创建对象的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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