使用内部构造函数实例化类 [英] Instancing a class with an internal constructor

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

问题描述

我有一个其构造函数定义为internal的类,这意味着我无法实例化它。尽管这可能很有意义,但我仍然愿意为调试和研究目的而这样做一次。

I have a class whose constructor is defined as internal, which means I cannot instantiate it. While that may make sense, I would still like to do it once for debugging and research purposes.

使用Reflection可以做到吗?我知道我可以访问私有/内部成员,但是我可以调用内部构造函数吗?

Is it possible to do so with Reflection? I know I can access Private/Internal Members, but can I call an internal constructor?

或者,由于构造函数没有什么重要意义,我可以使用反射来表示看似,只需在不调用构造函数的情况下为我提供类的实例,即可手动完成工作。

Or, as the constructor does nothing important, can I use reflection to say "Look, just give me an instance of the class without calling the constructor, I'll do it's work manually"?

性能和稳定性在这里不是问题,因为

Performance and "Stability" is not an issue here, as it's not production code.

编辑:正如澄清:不幸的是,我不控制另一个程序集,也没有它的源代码代码,我只是试图了解它的工作原理,因为它的文档几乎不存在,但是我应该与它交互。

Just as clarification: Sadly, I don't control the other assembly and don't have it's source code, I merely try to understand how it works as it's documentation is next to non-existent, but I am supposed to interface with it.

推荐答案

A FormatterServices.GetUninitializedObject 方法存在(名称空间:System.Runtime.Serialization),如果您确实想使用此方法,则它不调用任何构造函数尝试这种方法。

A FormatterServices.GetUninitializedObject method exists (Namespace: System.Runtime.Serialization), it supposedly calls no constructors, if you really want to try out that approach.

这篇关于使用内部构造函数实例化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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