问题描述
我有一个类的构造函数定义为内部,这意味着我无法实例化它.虽然这可能是有道理的,但为了调试和研究目的,我仍然想这样做一次.
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.
使用反射可以做到这一点吗?我知道我可以访问私有/内部成员,但我可以调用内部构造函数吗?
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.
这篇关于使用内部构造函数实例化一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!